有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    final String text = "allo every body how are you";
    int n = 3;
    final char toFind = 'y';
    
    int index = 0;
    while (index < text.length()) {
      if (text.charAt(index) == toFind) {
        n ;
        if (n == 0)
          break;
      }
      index++;
    }
    
    // index is 24
    System.out.print(index);
    
  2. # 2 楼答案

     public static int findIndex(String string, char c, int n)
     {
        int len = string.length();
        return IntStream.range(0, len)
                .filter(i -> c == string.charAt(i))
                .skip(n-1)
                .findFirst() 
                .orElse(-1); // No element found
     }