Java intercept backslash--java use split to split special characters

Source: Internet
Author: User

Java intercept backslash ReplaceAll and split ("\") Problem resolution

Xxx.split ("\ \") is obviously not getting the results you want.

Correct method

Xxx.split ("\\\\");

The reasons are as follows:

In Java to deal with some path-related problems, such as to remove the file name of Internet Explorer upload file, because IE will be the entire file path as file name upload, need to use java.lang.String in the ReplaceAll or split to deal with. Such as:

The upload file path is: C:\Documents and Settings\collin\my documents\111-lazyloading.gif, to remove the file name: 111-lazyloading.gif. OK

String temp[] = Name.split ("\\\\");
if (Temp.length > 1) {
name = Temp[temp.length-1];
}

The regex is \\\\ because \ \ is represented in Java and \ \ is also represented in the regex, so when \\\\ resolves to regex it is \ \.

Because File.separator is a slash "/" in Unix, the following code can handle all situations under Windows and UNIX:

Strin G temp[] = Name.replaceall ("\\\\", "/"). Split ("/");  


When you use split in Java to split special characters, you will find that you cannot achieve the results you want. Like what

Java code
    1. "1234567891^1234567890". Split ( "^") [1]  
 

An array subscript out-of-bounds exception is indicated, indicating that no split was successful at all. The reason, the original ^ is a special character, split parameter is a regular expression, so in order to let split identify special characters, you need to change the parameters to the regular, that is, before the parameter with "\ \" can be.

Java code
    1. "1234567891^1234567890". Split ("\\^") [1]

Java intercept backslash--java use split to split special characters

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.