我的字符串结构如下
字符1-3是大写字母,包括诸如Ň
字符4-7永远是数字.
第八是空间
9是正斜杠。
第10空间
11岁以上是号码。
String str1 = "DIW785o / 42"; // expected result "DIW7850 / 42"
String str2 = "QLR357Ï / 11"; // expected result "QLR3571 / 11"
String str3 = "UÜÈ7477 / 00"; // expected result "UÜÈ7477 / 00"
String str4 = "A / P8538 / 28"; // expected result "AÏP8538 / 28"
String str5 = "CV0875Z / 01"; // expected result "CVO8752 / 01"
String str6 = "SW / 2188 / 38"; // expected result "SWÏ2188 / 38"我想替换前三个字符,如
replaceAll("[2]", "Z")
.replaceAll("[0]", "O")
.replaceAll("[5]", "S")
.replaceAll(" // ","Ï) // replace space forward_slash space with Ï以及数字与以下数字的位置
.replaceAll("(?i)L|(?i)I", "1")
.replaceAll("(?i)o", "0")
.replaceAll("(?i)s", "5")
.replaceAll("(?i)z", "2") 发布于 2015-12-24 09:01:53
我想说没有正则表达式更容易,因为您想替换String,但只有当它们处于特定位置时:
检查/是否位于前7个字符中,并将其替换为Ï
if(input.indexOf(" / ") < 7 ){
input = input.replaceFirst(" / ", "Ï");
}那么你所有的弦都有相同的长度。现在将它们切成数字/字母部分,并替换您想要的所有内容:
String letterPart = input.substring(0,3);
String numberPart= input.substring(3,7);
String rest = input.substring(7);
letterPart = letterPart.replace("0", "O");
numberPart = numberPart.replace("o", "0");
numberPart = numberPart.replace("Ï", "1");
numberPart = numberPart.replace("Z", "2");然后把所有的东西重新组合起来:
String result = letterPart + numberPart + rest;https://stackoverflow.com/questions/34448033
复制相似问题