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!
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).
http://www.jsfiddle.net/a5SWU/
var a = "important, not so important";
a = a.split(",")[0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With