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];
}