Java Escape character Exploration recovery

Source: Internet
Author: User

At work today, there is a strange question about the escape and substitution of strings. Finally finally solved, special summary
Under

Small demo,

 PackageCom.westward; Public classDemo {/*** 1. Replace the string "a B" with "a 1b". AB is not a space, it is a tab.     * 2. Replace the string "A\TB" with "a 2b". * */     Public Static voidMain (string[] args) {//TODO auto-generated Method StubString string1= "A\TB"; System.out.println ("string1 =" +string1); String string2= "A\\TB"; System.out.println ("string2 =" +string2); System.out.println ("---------Conversion start-----------"); String1= String1.replaceall ("\\t", " 1"); string2= String2.replaceall ("\\\\t", " 2"); System.out.println ("Format:string1 =" +string1); System.out.println ("Format:string2 =" +string2); }}

Output
String1 =a b
string2 =A\TB
---------Conversion started-----------
Format:string1 =a 1b
Format:string2 =a 2b

Summarize:

In the Java language, the backslash \ In the string is the meaning of the escape character, not the backslash itself. So if we want to print out the backslash itself, we have to \ \. The first one is the escape character, which escapes the escaped word as the backslash itself, a bit around.

It is important to note that we print a string: xyz\txxx in the console, so we have to be sensitive to the recognition that the strings in the code are definitely not printed as they are, but should be xyz\\txxx. Similarly, if the console prints a string that is: xyz xxx, (in the middle is a tab), then the string in the code should be: xyz\txxx or xyz xxx.

Look at the demo above, when we need to replace a part of the string with another part, using the ReplaceAll () method of String, the first parameter of this method is the regular expression. Here we need to note:

In Java, \ is an escape character, in regular expressions (the regular expression itself is also equivalent to a language, not just Java, but also other languages), \ is also an escape character.  The regular expression in the \ t represents a tab, then in Java to represent this regular Expression tab, it must be represented: \\t. What if we need to represent the \ t itself in the regular, rather than the tab? We need to say this: \\\\t. In fact, it translates into a regular, is \\t, the first backslash escaped the second, the third backslash escaped fourth, in the regular, \\t the first backslash to escape the second backslash as the backslash itself, the same principle as the Java language.

Java Escape character Exploration recovery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.