简体   繁体   中英

How to replace Backslash '\' with double Backslash in Dart?

How to replace a Single Backslash '\\' in a String with double Backslash '\\' ?

I tried this, but its not working.

main(){
String string = "back\slash back\slash back\slash back\slash";
String replaced = string.replaceAll(RegExp(r'\\'), '\\\\');
print(replaced);
}

The problem is that the string string does not contain any \\

It would either need to be

String string = r"back\slash back\slash back\slash back\slash";

or

String string = "back\\slash back\\slash back\\slash back\\slash";

In your example there also is no need for RegExp. Just

String replaced = string.replaceAll(r'\', r'\\');

would do as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM