0

I am not familiar with Java program. The java Path comparison and string able to accomplish the task listed below ?

Path a = Paths.get("C:/Folder/");
Path b = Paths.get("C:/Folder/abc/def/");



Will there have any method to do a comparison for both path and retrieve only the difference between both of the path. For example, if i compare the a and b, i can detect /abc/def/ is the main differences from both of the path and store it into new variable. I had try search some code online but unfortunately the example i got is determine the path similarity and return the result yes or not only

Mordechai
  • 15,437
  • 2
  • 41
  • 82
attack
  • 103
  • 1
  • 11
  • 6
    Did you spend any time doing some research? Try [`Path#relativize()`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#relativize-java.nio.file.Path-) – Jim Garrison Jan 10 '17 at 06:06
  • @JimGarrison I just learned something. I gave an answer below using core String methods, but I guess Java already has this covered. – Tim Biegeleisen Jan 10 '17 at 06:08
  • How about marking this as a duplicate of http://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-from-two-absolute-paths-or-urls ? –  Jan 10 '17 at 06:33
  • Possible duplicate of [How to construct a relative path in Java from two absolute paths (or URLs)?](http://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-from-two-absolute-paths-or-urls). Answer for older and newer version are present so definitly a duplicate. – AxelH Jan 10 '17 at 06:39

4 Answers4

1

Use StringUtils from Apache Common API to diff string.

Here are some of the examples from documentation:

StringUtils.difference("ab", "abxyz") = "xyz"
StringUtils.difference("abcde", "abxyz") = "xyz"
StringUtils.difference("abcde", "xyz") = "xyz"

Get String from Path, and the difference:

String a = Paths.get("C:/Folder/").toString();
String b = Paths.get("C:/Folder/...").toString();
String diff = StringUtils.difference(a, b);
Che-Chia Chang
  • 291
  • 2
  • 10
0

One approach is to use basic Java string methods to determine if one path be contained by the other. If so, then take the extra substring of the containing path. Consider the following method:

public String findPathDiff(String patha, String pathb) {
    String diff = "";

    if (pathb.contains(patha)) {
        diff = pathb.substring(patha.length() - 1);
    }
    else if (patha.contains(pathb)) {
        diff = patha.substring(pathb.length() - 1);
    }
}

Usage:

String patha = "C:/Folder/";
String pathb = "C:/Folder/abc/def/";
String diff = findPathDiff(patha, pathb);
System.out.println(diff)

This would output /abc/def/ as being the difference between the two paths.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Just use

a.relativize(b)

The result would be: "abc\def\"

greg
  • 1,070
  • 7
  • 4
0

You can use the following StringUtils.difference(String a , String b) of org.apache.commons.lang.StringUtils .

Path a = Paths.get("C:/Folder/");
Path b = Paths.get("C:/Folder/abc/def/");
System.out.println(StringUtils.difference(a.toString(),b.toString());
Prashant
  • 167
  • 3
  • 13