Java Invalid escape sequence (valid ones is \b \ r \ n \f \ \ \ \ \ \)

Source: Internet
Author: User

Java escape character I always thought it was difficult to understand, I often confuse the \ symbol and/symbol, do not know that one is an escape character.

In the Java language you want to know \ is a hate a person's species, \ always like Su-eun love, generally are in pairs appear like \ \, but there will be a small number of wonderful flowers do not like the other half \, they like other characters, these wonderful characters and human homosexuality is not the same as most people like, They like to be special, such as \ and B together to represent the boundary of the character, \ r together to denote a carriage return, \ n means that the newline \ t represents a tab, and that the \f symbol is a separate \ symbol, all of which are specific symbols that have special meanings, which need to be remembered.

What does it mean to like Su-eun love? \ \ Together It's just a simple representation that a character is an escape, an escape to a character, or a representation of himself.

What scenarios need to be escaped? The most common is the regular expression, such as A|b|c.spilit ("|"), you think the result is a,b,c, the wrong result is not divided according to |, why not?

Because | The representation in Split or A|b&c#d.split ("&|#") This is based on the & or # Division result is a|b,c,d. What if I don't want to say or mean as long as he does it back to quiet himself? This time needs to add show love \ \, plus the escape character of the \\| represents the simple symbol |. There are also \w regular expressions that represent characters, \d represent numbers, and if you need regular expression matching \w cannot appear alone, you need \ \ to escape, so \\w actually represents \w.

Small program:

"S\\VF". Split ("\ \");

Here's the result of the S,VF, the answer is no. java prohibit \ \ appears as a regular expression, because \ \ in the regular expression does not mean only the expression transfer, only \\\\4 to represent himself, the first two \ \ Represents the transfer, the latter two \ \ represents itself.

So "S\\VF". Split ("\\\\") The result is S,VF


The last \ \ Character also has a special meaning that the root directory and//The meaning is the same, and/the meaning is not the same, on the code

public class Log4jtest {
public static void Main (string[] args) {

NPE
Log4jTest.class.getClassLoader (). GetResource ("/"). GetPath ();
/e:/cache/notice/target/classes/
Log4jTest.class.getClassLoader (). GetResource ("//"). GetPath ();

/e:/cache/notice/target/classes/%5c
Log4jTest.class.getClassLoader (). getresource ("\ \"). GetPath ();

Java Invalid escape sequence (valid ones is \b \ r \ n \f \ \ \ \ \ \)

Related Article

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.