Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String functions 2

Status
Not open for further replies.

jgos

Programmer
Sep 27, 2002
38
US
Does JavaScript have any functions like VB's left function and len function for strings? I want to change the last character on a comma-delimited string and I need something to do this.
 
you can do mostly everything VB can do in JS (as far as web pages are concerned).

to check the length of a string you can do :

var myString = "this is my test string"
alert(myString.length)

Also if you have a string like this one "test,test,test,test" you can change it to an array using split(',') and remove the last element in it.

var myString = "test,test,test,test";
alert("my original string " + myString)
var myArray = myString.split(',')
myArray.pop(); // removes the last element of my array
myString = myArray.join(','); // creates a coma delimited string
alert("now my string with the last element removed " + myString)

Does this help you out? Gary Haran
 
What I really want to do is change the value of a checkbox on a form. Right now I have a value on the checkbox that is a string of comma-delimeted parameters. I use these to build a query string to pass to a popup window that runs some asp code and returns values back to the parent page in javascript. I want to change the last character on the checkbox value from the child page so I need to run something like in vb:

myString = left(myString, Len(myString) -1) & "0"

Can javascript do anything like this?
 
i'm not sure what left does in VBscript.

However if all you want to do is change the last character of a string you can use substring.


var myString = "this is my string ";
myString = myString.substring(0, myString.length-1) + "0";
alert(myString)

I hope this helps! :) Gary Haran
 
star for two great examples with these two functions
xutopia


left simple returns a specified number of characters from the left side of a string.




A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top