I need to setup a function in javascript to remove the first character of a string but only if it is a comma ,
. I've found the substr
function but this will remove anything regardless of what it is.
My current code is
text.value = newvalue.substr(1);
replace Method. Another way to remove the leading commas from a string is to use the JavaScript string's replace method. We call replace with /^,/ to replace the leading comma with an empty string.
You can use the substring() method of java. lang. String class to remove the first or last character of String in Java. The substring() method is overloaded and provides a couple of versions that allows you to remove a character from any position in Java.
Remove Commas From String Using the re Package in Python In the re pacakge of Python, we have sub() method, which can also be used to remove the commas from a string. It replaces all the , in the string my_string with "" and removes all the commas in the string my_string .
text.value = newvalue.replace(/^,/, '');
Edit: Tested and true. This is just one way to do it, though.
s = (s.length && s[0] == ',') ? s.slice(1) : s;
Or with a regex:
s = s.replace(/^,/, '');
var result = (myString[0] == ',') ? myString.substr(1) : myString;
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