Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Sponsored Links

Discuss "remove comma" in the JavaScript Programming forum.

JavaScript Programming - Javascript is a cross-browser client-side scripting language. Discuss Javascript and AJAX related scripts here.


Closed Thread
 
LinkBack Thread Tools Display Modes
  #1  
Old May 7th, 2008, 09:55 AM
Barn Frequenter
 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default remove comma

Hi,

I have this variable strvar

alert(strvar);

the strvar variable holds values like this FDS, SDFR,

strvar=FDS, SDFR,

I want to remove the last comma after SDFR

Can someone tell me the javascript function for it

todd
Sponsored Links
  #2  
Old May 7th, 2008, 10:03 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

Try this:

Code:
strvar = strvar.substring(1, strvar.length-1);
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

  #3  
Old May 7th, 2008, 10:15 AM
Barn Frequenter
 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default

hi,

i want to show the cities selected by user seperated by comma

but i want the last comma to be removed

i tried ur code but it still displays the comma

Code:
function calldro()
{

len = document.frm1.cityopt.length
i = 0
strvar = ""
for (i = 0; i < len; i++) 
{
	if (document.frm1.cityopt[i].selected) 
		{
			strvar = strvar + document.frm1.cityopt[i].value + ", "
		} 
}
alert(strvar);
strvar = strvar.substring(1, strvar.length-1);
	
}
  #4  
Old May 7th, 2008, 10:17 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

You have to place that code before you use alert(strvar)
  #5  
Old May 7th, 2008, 10:25 AM
Barn Frequenter
 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default

i did jmurrayhead it didnt work out
  #6  
Old May 7th, 2008, 10:31 AM
mehere's Avatar
Super Sarcasm Mistress
 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 160
Thanks: 12
Thanked 29 Times in 27 Posts
Rep Power: 1
mehere will become famous soon enough

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default

from what i can see, when you're building that var .. you're adding a comma and a space between each word ... try this
Code:
}
strvar = strvar.substring(1, strvar.length-2);
alert(strvar);
}
__________________
Quote of the Month:
Quality: The race for quality has no finish line- so technically, it's more like a death march.

Questions to Ponder:
What do you do when you see an endangered animal eating an endangered plant?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69
  #7  
Old May 7th, 2008, 10:31 AM
Barn Frequenter
 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default

Jmuurayhead,

The code is cutting a character on the left side of the string and still keeping the comma at the end

i want it to cut the comma on the right side

todd
  #8  
Old May 7th, 2008, 10:35 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

Your code should look like this:
Code:
function calldro()
{

len = document.frm1.cityopt.length
i = 0
strvar = ""
for (i = 0; i < len; i++) 
{
	if (document.frm1.cityopt[i].selected) 
		{
			strvar = strvar + document.frm1.cityopt[i].value + ", "
		} 
}
 
strvar = strvar.substring(1, strvar.length-2);
alert(strvar);
 
}
because there is a space, you need to change strvar.length-1 to strvar.length-2 as mehere suggested.
  #9  
Old May 7th, 2008, 10:38 AM
Barn Frequenter
 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default

Ok,

Now here is the problem

this is my string


strvar=FDS, SDFR,

when i use the code you guys gave me this is the output

strvar=DS, SDFR

It removed the "F" also the first character in string

todd
  #10  
Old May 7th, 2008, 10:40 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

You're right...do this:
Code:
function calldro()
{

len = document.frm1.cityopt.length
i = 0
strvar = ""
for (i = 0; i < len; i++) 
{
	if (document.frm1.cityopt[i].selected) 
		{
			strvar = strvar + document.frm1.cityopt[i].value + ", "
		} 
}
 
strvar = strvar.substring(0, strvar.length-2);
alert(strvar);
 
}
Closed Thread

  DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -4. The time now is 12:06 AM.



Content Relevant URLs by vBSEO 3.2.0