Simply replace it with nothing:
var string = 'F0123456'; // just an example
string.replace(/^F0+/i, ''); '123456'
Answer from Mathias Bynens on Stack OverflowSimply replace it with nothing:
var string = 'F0123456'; // just an example
string.replace(/^F0+/i, ''); '123456'
Honestly I think this probably the most concise and least confusing, but maybe that is just me:
str = "F0123456";
str.replace("f0", "");
Dont even go the regular expression route and simply do a straight replace.
How can I remove a character from a string using JavaScript? - Stack Overflow
Remove character from string using javascript - Stack Overflow
Removing rest of string after a certain character
How to remove part of string after certain character in python?
There are a few ways. Here are a few off the top of my head:
>>> s = "abcd//efgh"
>>> s.find("/")
4
>>> s[:s.find("/")]
'abcd'
>>> s.split("/")
['abcd', '', 'efgh']
>>> s.split("/", maxsplit=1)
['abcd', '/efgh']
>>> s.split("/", maxsplit=1)[0]
'abcd'
>>> import re
>>> re.sub("/.*$", "", s)
'abcd'
The last is overkill here and I wouldn't use it, but regexs are often appropriate for doing search & replace operations. Either of the first two would work pretty well. The first depends on the search string appearing though. Otherwise, s.find will return -1 and then s[:-1] will lop off the last character:
>>> s = "abcdef"
>>> s[:s.find("/")]
'abcde' More on reddit.com What is the most efficient method to remove a character from a string in JavaScript?
How do I remove all instances of a specific character from a string?
When should I choose to use a custom function for character removal?
var mystring = "crt/r2002_2";
mystring = mystring.replace('/r','/');
will replace /r with / using String.prototype.replace.
Alternatively you could use regex with a global flag (as suggested by Erik Reppen & Sagar Gala, below) to replace all occurrences with
mystring = mystring.replace(/\/r/g, '/');
EDIT: Since everyone's having so much fun here and user1293504 doesn't seem to be coming back any time soon to answer clarifying questions, here's a method to remove the Nth character from a string:
String.prototype.removeCharAt = function (i) {
var tmp = this.split(''); // convert to an array
tmp.splice(i - 1 , 1); // remove 1 element from the array (adjusting for non-zero-indexed counts)
return tmp.join(''); // reconstruct the string
}
console.log("crt/r2002_2".removeCharAt(4));
Since user1293504 used the normal count instead of a zero-indexed count, we've got to remove 1 from the index, if you wish to use this to replicate how charAt works do not subtract 1 from the index on the 3rd line and use tmp.splice(i, 1) instead.
A simple functional javascript way would be
mystring = mystring.split('/r').join('/')
simple, fast, it replace globally and no need for functions or prototypes
JavaScript strings provide you with replace method which takes as a parameter a string of which the first instance is replaced or a RegEx, which if being global, replaces all instances.
Example:
var str = 'aba';
str.replace('a', ''); // results in 'ba'
str.replace(/a/g, ''); // results in 'b'
If you alert str - you will get back the same original string cause strings are immutable. You will need to assign it back to the string :
str = str.replace('a', '');
Use replace and if you want to remove multiple occurrence of the character use
replace like this
var test = "1,3,4,5,6,";
var newTest = test.replace(/,/g, '-');
here newTest will became "1-3-4-5-6-"