Answers for "how to replace multiple characters in a string"

1

replace many chracters js

// You can use regex to do it in only one replace
// Using the or: |
var str = '#this #is__ __#a test###__';
str.replace(/#|_/g,''); // result: "this is a test"
// Or using the character class 
str.replace(/[#_]/g,''); // result: "this is a test"
Posted by: Guest on September-21-2020
0

multiple replace

var str = '[T] and [Z] but not [T] and [Z]';
var result = str.replace('T',' ').replace('Z','');
console.log(result);
Posted by: Guest on May-12-2020
-2

replace multiple characters in a string

// Replace multiple characters in a string

fn main() {
    // Clumsy way
    let s = "The quick (brown) fox jumps over the lazy dog's back, and "runs" away; without barking: crazy.";
    let s = s.replace("(", "");
    let s = s.replace(")", "");
    let s = s.replace(",", "");
    let s = s.replace(""", "");
    let s = s.replace(".", "");
    let s = s.replace(";", "");
    let s = s.replace(":", "");
    let s = s.replace("'", "");
    println!("{}", s);

    // Better way
    let s = "The quick (brown) fox jumps over the lazy dog's back, and runs away; without barking: crazy.";
    let s = s.replace(&['(', ')', ',', '"', '.', ';', ':', '''][..], "");
    println!("{}", s);        
}
Posted by: Guest on October-13-2021

Code answers related to "how to replace multiple characters in a string"

Browse Popular Code Answers by Language