Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder Running Bear's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    104
    Thanks
    1
    Thanked 0 Times in 0 Posts

    [Resolved] How to Trim Last item off Delimited String?

    Hi,

    I've got a delimited string and want to trim off the last item. The easiest way I can thing of doing this is to split the string then remove or erase the last item then rejoin the string

    Any ideas on how to achieve this will be most welcome

    Cheers Al
    Last edited by Running Bear; 12-23-2005 at 09:00 AM. Reason: Resolved

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could use a regExp to do that. Here are a couple of tutorials for explaing how to use regExp.

    http://www.javascriptkit.com/javatutors/re.shtml

    http://www.javascriptkit.com/javatutors/redev.shtml

  • #3
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    	var nStr = "a,ckd,ih,987,hghf,i,iu,8ujhd,0";
    
    	nStr = nStr.substring(0,nStr.lastIndexOf(","));
    	
    	alert(nStr);
    
    
    </script>
    </head>
    <body>
    
    
    
    
    </body>
    </html>
    Last edited by Ancora; 12-21-2005 at 05:13 PM.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    Netscape

    Code:
    var
        sOrig='abc:def:ghi:jkl:mno:pqrs:tuv:wxyz:&#229;&#228;&#246;';
        sMod=sOrig.replace(/:[^:]*$/,'');
    // sOrig is now 'abc:def:ghi:jkl:mno:pqrs:tuv:wxyz:&#229;&#228;&#246;'
    // while sMod is 'abc:def:ghi:jkl:mno:pqrs:tuv:wxyz'
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder Running Bear's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    104
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks a bunch for your replys, really appreciated.

    I did manage to do it using the method I described in my first post but, your methods are a lot shorter and cleaner

    The question now is which is the more efficient?

    liorean's Regular Expression
    Or
    Ancora's Substring and LastIndexof?

    This what I had which is going to binned in favour of one of the above

    Code:
    Function UnDo(){
    		var sTo=document.myForm.txtTo.value;
    		var uBnd
    		var Temp = New Array();
    				
    		Temp=sTo.split(";");
    		uBnd=Temp.length-1;
    		Temp.splice(uBnd,1);
    		sTo=Temp.join(";");
    		document.myForm.txtTo.value=sTo;
    		
    	}
    Last edited by Running Bear; 12-22-2005 at 03:28 PM.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    I would expect the two methods to be as efficient as one another since they are simply two ways of asking the browser to do the exact same thing. It depends on how the browsers implement the individual functions as to which will run faster on a given browser.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Regular Coder Running Bear's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    104
    Thanks
    1
    Thanked 0 Times in 0 Posts

    [Resolved] How to Trim Last item off Delimited String?

    Stephen,

    Thanks for that.

    Cheers Al


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •