我需要返回字符串中字符的唯一值。我正在使用String.indexOf(),但它对我来说不正确。
例如:相机
索引: c-0,a-1,m-2,e-3,r4,a-5
String.indexOf()总是为字母"a“返回索引1,而不是我所需要的1和5。
有什么办法解决吗?
发布于 2014-11-21 22:39:47
public static void main(String[] args) {
List<Integer> indexes = getIndexInString("camera", "a");
System.out.println(indexes);
}
public static List<Integer> getIndexInString(String str, String charToFind) {
List<Integer> indexes = new ArrayList<Integer>();
int i = -1;
while (true) {
i = str.indexOf(charToFind, i + 1);
if (i == -1)
break;
else
indexes.add(i);
}
return indexes;
}https://stackoverflow.com/questions/27071430
复制相似问题