I cant seem to find a solution.
I have two paths, like:
D:/Views/me/a.b AND
D:/Views/me/a
or
D:/Views/me/a.b AND
D:/Views/me/a.b/x/y
I have to ensure, that one file/directory is not a child of the other.
I have tried Contains but it does not work for me in this case?
解决方案
Try using startsWith api of String:
String str1 = "D:/Views/me/a.b";
String str2 = "D:/Views/me/a";
if (str1.startsWith(str2 + ".") || str1.startsWith(str2 + "/")) {
System.out.println("Yes it is");
}
str1 = "D:/Views/me/a/c/d";
str2 = "D:/Views/me/a";
if (str1.startsWith(str2 + ".") || str1.startsWith(str2 + "/")) {
System.out.println("Yes it is");
}
Output:
Yes it is
Yes it is