When \ is used to separate strings

Source: Internet
Author: User

I found that there are still some things in my mind, and there is no concept after a period of time, so I posted them so as not to forget...

When splitting a string, an exception is reported when \ is used to split the string.

Public class main {/*** @ Param ARGs */public static void main (string [] ARGs) {string AA = "G: \ A \ BB \ cc.gif "; system. out. println (AA);/*** because it involves two layers, one is Java and the other is source code. * The regular expression needs to be escaped and \ is used to represent \; * In the source code of Java, escape characters must be expressed \. * Therefore, when they are accumulated, they must be four diagonal lines \. In this way, the Java escape character is \\, and \ is escaped in the Regular Expression and then \ can be matched * A little round, haha ~ */String [] xx = AA. Split ("\"); For (string cell: XX) {system. Out. println (cell );}}}

The exception is as follows:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1\ ^at java.util.regex.Pattern.error(Pattern.java:1713)at java.util.regex.Pattern.compile(Pattern.java:1466)at java.util.regex.Pattern.<init>(Pattern.java:1133)at java.util.regex.Pattern.compile(Pattern.java:823)at java.lang.String.split(String.java:2292)at java.lang.String.split(String.java:2334)at cn.osl.businessagent.test.Main.main(Main.java:18)

In this case, use \\\\

Public class main {/*** @ Param ARGs */public static void main (string [] ARGs) {string AA = "G: \ A \ BB \ cc.gif "; system. out. println (AA);/*** because it involves two layers, one is Java and the other is source code. * The regular expression needs to be escaped and \ is used to represent \; * In the source code of Java, escape characters must be expressed \. * Therefore, when they are accumulated, they must be four diagonal lines \. In this way, the Java escape character is \\, and \ is escaped in the Regular Expression and then \ can be matched * A little round, haha ~ */String [] xx = AA. Split ("\\\\"); For (string cell: XX) {system. Out. println (cell );}}}

Output result:

g:\a\bb\cc.gifg:abbcc.gif

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.