开发者

Printing Special Escape Sequences

开发者 https://www.devze.com 2023-02-16 12:56 出处:网络
I have a string (pulled from the web) wh开发者_运维百科ich may look like this: \\something\\to\\do. When I print this string via System.out.println \\s \\t and \\d are treated as escape sequences. How

I have a string (pulled from the web) wh开发者_运维百科ich may look like this: \something\to\do. When I print this string via System.out.println \s \t and \d are treated as escape sequences. How can I print the sting in such a way so that escape sequences are ignored?


If you want to have the following output:

\something\to\do

Then you need to double backslash the backslash characters to make them into backslash literals:

string myString = "\\something\\to\\do";


You have to escape the backslashes:

String s = "\something\to\do";
System.out.println(s.replaceAll("\\", "\\\\")); // prints "\something\to\do"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号