Need help in replacing character '\' in a string

Dear all,

I am trying to do something like this

fn main() {
let s = "{{\"bssid\" : \"c4:e9:84:f1:b6:46\"}}";
println!("replaced {:?}", s.replace("\\", "test"));
}

The result is s "{{"bssid" : "c4:e9:84:f1:b6:46"}}"
However I want the \ in the s to be replace with nothing. I want to have something like
"{{"bssid" : "c4:e9:84:f1:b6:46"}}"

Your original string has no such char , what you see in the code or the output are the escape characters.

If you encode the original string s as raw-string it will contain the char \
Now, when you replace this backslash char \ now the resulting string is shorter.

fn main() {
# encode as raw-string
let s = r#"{{\"bssid\"}}"#;
let t = s.replace("\\", "");
println!("replaced origlen={} newlen={} {:?}", s.len(), t.len(), t);
}

the out output will be

replaced origlen=13 newlen=11 "{{\"bssid\"}}"
1 Like

Thanks. I got the problem. It looks like that it is seen only in the output