Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  46] [ 3]  / answers: 1 / hits: 25457  / 14 Years ago, fri, february 11, 2011, 12:00:00

I'm looking for a way to remove the comma and all that comes after it in a string, for example:




important, not so important




I'd like to remove ,not so important



Any ideas? Thanks in advance!


More From » jquery

 Answers
27

You can do it with substring and indexOf:



str = str.substring(0, str.indexOf(','));


but you'd have to be sure that a comma is in there (test it before).



Another possibility is to use split():



str = str.split(',')[0];


this works even without testing beforehand but might perform unnecessary string operations (which is probably negligible on small strings).


[#93775] Thursday, February 10, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;