Home > database >  How to replace a character in a string?
How to replace a character in a string?

Time:10-31

The question is, that if i have a string, i.e:

"--Julius.",

and I want replace "-" and ".". How i can get it?

Finally, I get:

"Julius"

CodePudding user response:

This question have an easy solution. You only do the next:

word="--Julius."
word.replace("--", "")
word.replace(".", "")
print(word)

Output:

"Julius"

CodePudding user response:

This is specific for the javascipt.

var str ='--Julius.';
str = str.replace(/[-.]/g, function(m) { return {'-':'','.':''}[m]; }); 
console.log(s);

we can use the .replace(oldvalue,newvalue) function and replace multiple characters in a string in the similar manner.

var str ='--Julius.';
str = str.replace(/[-.]/g, ''); 
console.log(str);

we could use this instead to replace all the character with the same character.

  • Related