Tim
Tim

Reputation: 1769

Join path that overlaps

In java, I have a string that looks like :

"c:\abc\def\ghi"

and another that is

"def\ghi\jkl.txt"

How can I do the intersection of both to have

"c:\abc\def\ghi\jkl.txt" 

edit :

The rules are :

replace the maximum of the end of the first string with the maximum of the beginning of the second string.

For example with

Upvotes: 0

Views: 145

Answers (1)

F. Fritz
F. Fritz

Reputation: 69

I would simply look at the first string and check if it ends with the largest possible beginning of the second string. To be a little bit faster I check on every existing backslash:

public static String join(String begin, String end) {
    for (int i = end.lastIndexOf("\\"); i >= 0; i = end.lastIndexOf("\\", i - 1)) {
        if (begin.endsWith(end.substring(0, i)) && begin.charAt(begin.length() - (i+1)) == '\\') {
            return begin + end.substring(i);
        }
    }
    return "strings dont contain same folder sequence";
}

Upvotes: 1

Related Questions