您的位置:首页 > 其它

String中查询重复字符

2016-11-04 10:19 141 查看
@Test
public void test1() {
String str = "aaaaddaaddababacaxaxcccaswfdsfasgdfah";
//把SIZE提前取出,免得在循环中每次都得取
int charSize = str.length();
Set<Object> rSet = new HashSet<>();
for (int j = 1; j <= charSize; j++) {
for (int i = 0; i < charSize; i++) {
String strE2 = null;
if (i < charSize - j) {
//截取的波动区间为(1~SIZE-1)
strE2 = str.substring(i, i + j);
System.out.println( "---" + strE2 + "---" );
//正向查找,反向查找,如果能查到则返回对应的位置
//因为是从str中截取的strE2,所以不会有indexOf()==-1的情况
int indexS = str.indexOf(strE2);
int indexE = str.lastIndexOf(strE2);
if(indexS != indexE){
//使用Set的特性过滤并保存重复的值
rSet.add(strE2);
}
}
}
}
System.out.println(rSet);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐