The Java string cutting instance learns of to get the file name

  • 2020-04-01 02:35:08
  • OfStack

The upload file path is: C:/Documents and Settings/collin/My Documents/ 111-lazyload.gif. can


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

A regex is ///, because // in Java represents a /, and // in regex also represents /, so when /// is resolved into a regex, it is //.

Since file.separator is a slash "/" in Unix, the following code handles all cases on Windows and Unix:


String temp[] = name.replaceAll("////","/").split("/");
if (temp.length > 1) {
    name = temp[temp.length - 1];
}


Related articles: