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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String remove last ; semicolon

Status
Not open for further replies.

DH

Programmer
Dec 8, 2000
168
I am not to familar with JavaScript and would like to:

1.) test if a string ends with a semicolon ;
2.) if string ends in semicolon ; then remove the semicolon ;

Original String:
email1@domain.com; email2@domain.com;

I would like to end up with the following string:
email1@domain.com; email2@domain.com

Does anyone have an example or source code they could post.

Thank you!

DH
 
Just for a bit of fun with regexp.
[tt]
var s="email1@domain.com; email2@domain.com;"
var b=/;$/.test(s); //return true if end with ; false if not.
var t=s.replace(/;$/,""); //by itself always elim ; at the end if any
[/tt]
You sure can do as well with string functions like substring(), indexOf() etc.
 
tsuji thanks for the help!

Can I ask you one more question...

I create a variable for the value of a textbox like so:

var emailString = document.Form1.txtEmail.value;

I would like to test the variable to see if the variable ends with a semicolon and if not concatenate a semicolon to the variable

I can accomplish this in asp.net/vb.net but I don't use JavaScript much and am getting stuck.

Once this is solved I can add the ; to the string when needed and remove it when needed.

Thanks again!

DH
 
Here you go. I set this up as a function so you can pass a value to it to have it tested and have that value returned. If there is a semicolon on the end it gets removed from the returned string and if not it returns the original string.

function checkstr(mystr){
var outstr = ((mystr.charAt(mystr.length-1,1) == ";") ? mystr.substring(0,mystr.length-1) : mystr);
return outstr;
}

alert(checkstr("ABCDEFGH;"));

This is a short way to do it but is not the clearest to understand. It uses a conditional operator to do the compare. The variable outstr will get the output.
First it uses mystr.charAt(mystr.length-1,1) which checks a single character starting at the last character in the string and tests if it is == ";". If this test is true then the operand after the ? is used, if it is false the operand after the : is used.
The second operand grabs the string starting at position 0 up to the end of the string minus 1 character.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Damn, tsuji beat me. tsuji, go to bed it's late.

On second thought, never mind, "I" will go to bed instead.

Paranoid? ME?? WHO WANTS TO KNOW????
 
Maybe something like this. [1] To add...
[tt]
if (!/;$/.test(emailString)) {emailString+=";"}
[/tt]
[2] To elim...
[tt]
if (/;$/.test(emailString)) {
emailString=emailString.replace(/;$/,"")
}[/tt]
 
theniteowl, me go to bed already? I think I get up late! Take care.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top