DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

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 « Previous Thread | Next Thread »
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old May 7th, 2008, 09:55 AM
Contributing Member

 
Join Date: Mar 2008
Posts: 174
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 (permalink)  
Old May 7th, 2008, 10:03 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 534
Thanks: 14
Thanked 39 Times in 38 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

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

Default

Try this:

Code:
strvar = strvar.substring(1, strvar.length-1);
__________________
jmurrayhead
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
  #3 (permalink)  
Old May 7th, 2008, 10:15 AM
Contributing Member

 
Join Date: Mar 2008
Posts: 174
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 (permalink)  
Old May 7th, 2008, 10:17 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 534
Thanks: 14
Thanked 39 Times in 38 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

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

Default

You have to place that code before you use alert(strvar)
  #5 (permalink)  
Old May 7th, 2008, 10:25 AM
Contributing Member

 
Join Date: Mar 2008
Posts: 174
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 (permalink)  
Old May 7th, 2008, 10:31 AM
mehere's Avatar
Super Mistress of Sarcasm

 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 111
Thanks: 8
Thanked 20 Times in 18 Posts
Rep Power: 1
mehere is on a distinguished road

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:
Strife: As long as we have each other, we'll never run out of problems.

Questions to Ponder:
Should vegetarians eat animal crackers?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69
  #7 (permalink)  
Old May 7th, 2008, 10:31 AM
Contributing Member

 
Join Date: Mar 2008
Posts: 174
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 (permalink)  
Old May 7th, 2008, 10:35 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 534
Thanks: 14
Thanked 39 Times in 38 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft SQL Server Microsoft Windows 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 (permalink)  
Old May 7th, 2008, 10:38 AM
Contributing Member

 
Join Date: Mar 2008
Posts: 174
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 (permalink)  
Old May 7th, 2008, 10:40 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 534
Thanks: 14
Thanked 39 Times in 38 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft SQL Server Microsoft Windows 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


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 10:35 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46