索引string中所有出现的字符
以下代码将打印2
String word = "bannanas"; String guess = "n"; int index; System.out.println( index = word.indexOf(guess) );
我想知道如何在string“bannanas”中获得“n”(“guess”)的所有索引,
预期的结果是: [2,3,5]
这应该在Peter Lawrey的解决scheme结束时打印出没有-1
的位置列表。
int index = word.indexOf(guess); while (index >= 0) { System.out.println(index); index = word.indexOf(guess, index + 1); }
它也可以作为for
循环来完成:
for (int index = word.indexOf(guess); index >= 0; index = word.indexOf(guess, index + 1)) { System.out.println(index); }
[注意:如果guess
可能比单个字符长,那么通过分析guess
string,可以比上述循环更快地循环word
。 这种方法的基准是Boyer-Moorealgorithm 。 但是,有利于采用这种方法的条件似乎并不存在。]
尝试以下(最后不打印-1)!
int index = word.indexOf(guess); while(index >= 0) { System.out.println(index); index = word.indexOf(guess, index+1); }
String string = "bannanas"; ArrayList<Integer> list = new ArrayList<Integer>(); char character = 'n'; for(int i = 0; i < string.length(); i++){ if(string.charAt(i) == character){ list.add(i); } }
结果将会像这样使用:
for(Integer i : list){ System.out.println(i); }
或者作为一个数组:
list.toArray();
int index = -1; while((index = text.indexOf("on", index + 1)) >= 0) { LOG.d("index=" + index); }
String word = "bannanas"; String guess = "n"; String temp = word; while(temp.indexOf(guess) != -1) { int index = temp.indexOf(guess); System.out.println(index); temp = temp.substring(index + 1); }
String input = "GATATATGCG"; String substring = "G"; String temp = input; String indexOF =""; int tempIntex=1; while(temp.indexOf(substring) != -1) { int index = temp.indexOf(substring); indexOF +=(index+tempIntex)+" "; tempIntex+=(index+1); temp = temp.substring(index + 1); } Log.e("indexOf ","" + indexOF);
另外,如果你想find一个string中的所有索引的string。
int index = word.indexOf(guess); while (index >= 0) { System.out.println(index); index = word.indexOf(guess, index + guess.length()); }
我也有这个问题,直到我想出了这个方法。
public static int[] indexesOf(String s, String flag) { int flagLen = flag.length(); String current = s; int[] res = new int[s.length()]; int count = 0; int base = 0; while(current.contains(flag)) { int index = current.indexOf(flag); res[count] = index + base; base += index + flagLen; current = current.substring(current.indexOf(flag) + flagLen, current.length()); ++ count; } return Arrays.copyOf(res, count); }
这个方法可以用来查找string中任何长度的任何标志的索引,例如:
public class Main { public static void main(String[] args) { int[] indexes = indexesOf("Hello, yellow jello", "ll"); // Prints [2, 9, 16] System.out.println(Arrays.toString(indexes)); } public static int[] indexesOf(String s, String flag) { int flagLen = flag.length(); String current = s; int[] res = new int[s.length()]; int count = 0; int base = 0; while(current.contains(flag)) { int index = current.indexOf(flag); res[count] = index + base; base += index + flagLen; current = current.substring(current.indexOf(flag) + flagLen, current.length()); ++ count; } return Arrays.copyOf(res, count); } }
一个分裂的string我想出了。 最后提供一个简短的testing。
如果可能, SplitStringUtils.smartSplitToShorterStrings(String str, int maxLen, int maxParts)
将按空格拆分,如果不可能,将根据maxLen按索引拆分。
提供其他方法来控制它是如何分割的: bruteSplitLimit(String str, int maxLen, int maxParts)
, spaceSplit(String str, int maxLen, int maxParts)
。
public class SplitStringUtils { public static String[] smartSplitToShorterStrings(String str, int maxLen, int maxParts) { if (str.length() <= maxLen) { return new String[] {str}; } if (str.length() > maxLen*maxParts) { return bruteSplitLimit(str, maxLen, maxParts); } String[] res = spaceSplit(str, maxLen, maxParts); if (res != null) { return res; } return bruteSplitLimit(str, maxLen, maxParts); } public static String[] bruteSplitLimit(String str, int maxLen, int maxParts) { String[] bruteArr = bruteSplit(str, maxLen); String[] ret = Arrays.stream(bruteArr) .limit(maxParts) .collect(Collectors.toList()) .toArray(new String[maxParts]); return ret; } public static String[] bruteSplit(String name, int maxLen) { List<String> res = new ArrayList<>(); int start =0; int end = maxLen; while (end <= name.length()) { String substr = name.substring(start, end); res.add(substr); start = end; end +=maxLen; } String substr = name.substring(start, name.length()); res.add(substr); return res.toArray(new String[res.size()]); } public static String[] spaceSplit(String str, int maxLen, int maxParts) { List<Integer> spaceIndexes = findSplitPoints(str, ' '); List<Integer> goodSplitIndexes = new ArrayList<>(); int goodIndex = -1; int curPartMax = maxLen; for (int i=0; i< spaceIndexes.size(); i++) { int idx = spaceIndexes.get(i); if (idx < curPartMax) { goodIndex = idx; } else { goodSplitIndexes.add(goodIndex+1); curPartMax = goodIndex+1+maxLen; } } if (goodSplitIndexes.get(goodSplitIndexes.size()-1) != str.length()) { goodSplitIndexes.add(str.length()); } if (goodSplitIndexes.size()<=maxParts) { List<String> res = new ArrayList<>(); int start = 0; for (int i=0; i<goodSplitIndexes.size(); i++) { int end = goodSplitIndexes.get(i); if (end-start > maxLen) { return null; } res.add(str.substring(start, end)); start = end; } return res.toArray(new String[res.size()]); } return null; } private static List<Integer> findSplitPoints(String str, char c) { List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == c) { list.add(i); } } list.add(str.length()); return list; } }
简单的testing代码:
public static void main(String[] args) { String [] testStrings = { "123", "123 123 123 1123 123 123 123 123 123 123", "123 54123 5123 513 54w567 3567 e56 73w45 63 567356 735687 4678 4678 u4678 u4678 56rt64w5 6546345", "1345678934576235784620957029356723578946", "12764444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444", "3463356 35673567567 3567 35 3567 35 675 653 673567 777777777777777777777777777777777777777777777777777777777777777777" }; int max = 35; int maxparts = 2; for (String str : testStrings) { System.out.println("TEST\n |"+str+"|"); printSplitDetails(max, maxparts); String[] res = smartSplitToShorterStrings(str, max, maxparts); for (int i=0; i< res.length;i++) { System.out.println(" "+i+": "+res[i]); } System.out.println("==========================================================================================================================================================="); } } static void printSplitDetails(int max, int maxparts) { System.out.print(" X: "); for (int i=0; i<max*maxparts; i++) { if (i%max == 0) { System.out.print("|"); } else { System.out.print("-"); } } System.out.println(); }
这可以通过迭代myString
和indexOf()
fromIndex
参数来完成:
int currentIndex = 0; while ( myString.indexOf( mySubstring, currentIndex) >= 0) { System.out.println(currentIndex); currentIndex++; }
尝试这个
String str = "helloslkhellodjladfjhello"; String findStr = "hello"; System.out.println(StringUtils.countMatches(str, findStr));