【问题标题】:how to find in java string case incesitive number of word java如何在java字符串大小写中查找单词java的incesitive数
【发布时间】:2025-11-28 07:45:01
【问题描述】:

skdoakdoakdoakdpoakdpoakdopkapodkoapdkpoakdpoakdpakpdkapdkpaodkpakdpakdpakdpkpdkadpkapdkpaoooooooooooooooooooooooooooooooooooooooooooooooo dksaodaoddddddddddddddddddddddddddddddddddkaopdkpadpaokdakdopakdpakpda dksaopdoapkdpakdpoakdpokdkapodpadpakdpoakdopakdopkpoakdpapdkapdpada kdposaodpadpakdopakdpoakdpokpdkpakdopadkpakdkdkpadkakdpakdp

       String test1 = "java java java. JAVA! ";
       String test2 = "h w !!! ...";
       String word ="java";
       int count = 0;

      String strArray[] = test2.split(" ");

      for(int i=0; i < strArray.length; i++){
            if (word.equals("java")) {
                `enter code here`   
                 count++;
            }            
            System.out.println(strArray[i]);
      }

    System.out.println(count);
    test1.toUpperCase() + test1.toLowerCase().concat;
    count++;
    test1.toLowerCase();
    count++;
    System.out.println(count);

如何在 java 中返回不区分大小写的 java 中的出现我会在互联网上拖钓,但没有运气。在 JavaScript 中我会使用 are 但在这里我是新的所以请帮忙。所以我需要按照上面的建议找到java不区分大小写的次数

【问题讨论】:

  • 看看这是否有帮助.. *.com/questions/767759/….只改变“lastIndex = str.indexOf(findStr,lastIndex);”到“lastIndex = str.toLowerCase().indexOf(findStr,lastIndex);”

标签: java string comparison case-insensitive


【解决方案1】:

希望这会有所帮助

String inputString = "java java java. JAVA!";

        int counter = 0;
        Pattern pattern = Pattern.compile("JAVA");
        Matcher match = pattern.matcher(inputString.toUpperCase());
        while (match.find()) {
            counter++;
        }

        System.out.println(counter);

【讨论】:

    【解决方案2】:
    enter code here:
        String regex = "JAVA";
        String input =  "Java java JAVA ss".toUpperCase();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(input);
        int count = 0;
        List<Integer> index = new ArrayList<>();
        while (m.find()) {
            count++;
            System.out.println("Match number: " + count);
            System.out.println("start: " + m.start());
            System.out.println("end: " + m.end());
    
            index.add(m.start());
        }
        System.out.println("count: " + count);
        System.out.println("index: " + index.toString());
    

    第一次用这个,希望对你有帮助。

    【讨论】:

      【解决方案3】:

      为了良好的秩序:

          Pattern pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
          Matcher matcher = pattern.matcher(inputString);
          int count = (int) matcher.results().count();
      

      或简称:

          int count = Pattern.compile("java", Pattern.CASE_INSENSITIVE)
                  .matcher(inputString).results().count();
      

      【讨论】: