...

View Full Version : [Resolved] How to Trim Last item off Delimited String?



Running Bear
12-21-2005, 04:27 PM
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

PhotoJoe47
12-21-2005, 04:37 PM
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

Ancora
12-21-2005, 05:08 PM
<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>

liorean
12-21-2005, 05:27 PM
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'

Running Bear
12-22-2005, 12:19 PM
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


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;

}

felgall
12-22-2005, 09:43 PM
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.

Running Bear
12-23-2005, 09:01 AM
Stephen,

Thanks for that.

Cheers Al



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum