Normalize a path to prevent multiple '/' occurrences in a path, and prevent ' \ ' from appearing in the path, preventing the path from ending with '/'

Source: Internet
Author: User

Because of different systems, the path information is different, but the final processing is in the same format,

For example: The input path is: D:\\kassdev\\app-kaace\\classes\\com\\kass\\commons\\utils,

The path information required for the output is: d:/kassdev/app-kaace/classes/com/kass/commons/utils

The following code, which deals with this situation

public static string Parsepath (final String path) {
if (Path==null | | path.length () ==0)
Return "";
if (path.indexof ("\ \") >=0) {
String path2 = Path.replaceall ("\\\\", "/");
while (Path2.indexof ("//") >=0) {
path2 = Path2.replaceall ("//", "/");
}
if (Path2.endswith ("/") &&!path2.equals ("/")) {
path2 = path2.substring (0, Path2.length ()-1);
}
return path2;
}else{
if (Path.indexof ("//") >=0) {
String path2 = Path.replaceall ("//", "/");
while (Path2.indexof ("//") >=0) {
path2 = Path2.replaceall ("//", "/");
}
if (Path2.endswith ("/") &&!path2.equals ("/")) {
path2 = path2.substring (0, Path2.length ()-1);
}
return path2;
}else{
if (Path.endswith ("/") &&!path.equals ("/")) {
String path2 = path.substring (0, Path.length ()-1);
return path2;
}else{
return path;
}
}
}
}

Normalize a path to prevent multiple '/' occurrences in a path, and prevent ' \ ' from appearing in the path, preventing the path from ending with '/'

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.