字符串中只能有且只有一处连续的数字不同,非数字必须相同(前面有不同数字,下标可以不同,否则下标必须相同。),
A001B001 与A002B001 OK
A001B001 与A002B002 NO 两处不同
A001B001 与A02B001 OK 001与02长度可以不同
001 与002 OK
B01 与B0000002 OK
B001B01 与B002A01 NO B与A是非数字,必须相同
B001B01 与B2B01 OK B与B的下标可以不同,因为前面有不同的数字,
B001B01 与B001A02 NO B与A是非数字,因为前面没有不同的数字,下标必须相同,
public class VersionName {
public static void main(String[] args) {
String[] str1 = { "B001", "B001N001", "B001N001","B001N001", "A001BBB001", "A002B", "A001BBB","A001B000","A001BBB","A001B0002","001AA001","AAA" ,"0001","000","AAA","", ""};
String[] str2 = { "B002", "B002N001", "B01N001", "B01N002", "A002BBB002", "A001BBB", "A001B", "A001B000004","A001BBB", "A001B01", "01AA001", "AAA" ,"0002","000","AAB","1",""};
VersionName version = new VersionName();
for (int i = 0; i < str1.length; ++i) {
version.getDifferent(str1[i], str2[i]);
}
}
public String getDifferent(String versionA, String versionB) {
System.out.print(versionA + "与" + versionB+":");
boolean isDifferent = false;
int beginA = -1, beginB = -1, lengthA = versionA.length(), lengthB = versionB.length();
String numberA = "", numberB = "";
int i = 0, j = 0, index = 0;
for (; i < lengthA && j < lengthB;) {
char ch = versionA.charAt(i);
if (Character.isLetter(ch)) {
if (ch == versionB.charAt(j)) {
++i;
++j;
} else {
System.out.println(ch + "字母不同" + versionB.charAt(j)+false);
return null;
}
} else {
beginA = i;
beginB = j;
while ((++i) < lengthA && Character.isDigit(versionA.charAt(i))) {
}
while ((++j) < lengthB && Character.isDigit(versionB.charAt(j))) {
}
if (i >= lengthA) {
i = lengthA;
}
if (j >= lengthB) {
j = lengthB;
}
String numberTemA = versionA.substring(beginA, i);
String numberTemB = versionB.substring(beginB, j);
if (numberTemA.equals(numberTemB)) {
} else {
if (isDifferent) {
System.out.println("不止一处不同" + numberA + "," + numberB + "," + numberTemA + "," + numberTemB+false);
return null;
} else {
isDifferent = true;
index = beginA;
numberA = numberTemA;
numberB = numberTemB;
}
}
}
}
if (isDifferent && i >= versionA.length() && j >= versionB.length()) {
String tem = index + ":" + numberA + ":" + numberB;
System.out.println(" " + tem + " " + true);
return tem;
} else {
System.out.println(" " + false);
return null;
}
}
}