JAVA去除字符串中重复字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void main(String[] args) {
String s1 = "善南街道善国苑善国苑24号楼12单元14层1402";
String s2 = "枣庄滕州市龙泉街道苹果花园苹果花园9号楼3单元18层144023";
String s3 = "枣庄滕州市张汪镇邱仓/闫道沟邱仓/闫道沟";
String s = "枣庄滕州市西岗镇西岗镇沿街市场西岗镇沿街市场423号楼34单元12342号箱10235";
int l = 2;
String congfu="";
for (int i = 0; i < s.length() - l; i++) {
String ss = s.substring(i, i + l + 1);
String sss = s.replaceFirst(ss, "");
if (sss.contains(ss)) {
congfu=ss;
l++;
i--;
}
}
System.out.println(congfu);
s=s.replaceFirst(congfu, "");
System.out.println(s);
}

注意:字符串中含有“\”时

1
2
string.contains("\\");
string.replace("\\\\","");

分享到 评论