Java string cutting example

Source: Internet
Author: User

Java string cutting example

When dealing with Path-related issues in Java, for example, to retrieve the file name uploaded by ie browser, because ie will upload the entire file path as a file name, you need to use java. lang. replaceAll or split in String to process the data. The following describes how to use it.

The file to be uploaded is in the path C:/Documents and Settings/collin/My Documents/111-lazyloading.gif. The file to be uploaded is named 111-lazyloading.gif. Yes

 

 

The Code is as follows:

String temp [] = name. split ("////");

If (temp. length> 1 ){

Name = temp [temp. length-1];

}

 

 

The regex is //, because in java, // represents a //, while in regex, // also represents /, therefore, when/is resolved to regex, It is //.

 

Since file. separator in unix is a slash "/", the following code can handle all windows and unix scenarios:

 

 

The Code is as follows:

String temp [] = name. replaceAll ("//", "/"). split ("/");

If (temp. length> 1 ){

Name = temp [temp. length-1];

}

 

 

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.