java - how to compare an string array? -
i have code example. want compare strings contains paths. problem @ else part...in example have / , usr together..and want compare length after point different..thank help
string path1 = "/usr/libexec/warmd"; string path2 = "/usr/lib/libopen/xy"; string[] test1 = path1.split("/"); string[] test2 = path2.split("/"); arraylist<string[]> path1array = new arraylist<string[]>(); arraylist<string[]> path2array = new arraylist<string[]>(); path1array.add(test1); path2array.add(test2); int len1 = test1.length; int len2 = test2.length; int len; if (len1 <= len2) { len = len1; } else len = len2; (int = 0; < len; i++) { if ((test1[i]).equals(test2[i])) { continue; } else break; }
you can find common prefix substring follows :
int minlen = math.min(test1.length, test2.length); string commonprefix = ""; (int = 0; < minlen; i++) { if (test1[i].compareto(test2[i]) == 0) { commonprefix += test1[i]+"/"; } else { break; } }
if don't string assignments, use stringbuffer instaed. paths :
string path1unique = path1.replacefirst(commonprefix, ""); string path2unique = path2.replacefirst(commonprefix, "");
Comments
Post a Comment