在 java 中使字符串不区分大小写

Make the string case insensitive in java

我在我的应用程序中有一个问题我有一个 String 其中包含一个搜索词,然后我使用这个词从一个长文本中搜索,我希望所有包含搜索词的词return(例如 WoRd = word = WORD)。谢谢。

private static int countWords(String tstr1, String string)
    {   
        int i = 0;
        Scanner s = new Scanner(tstr1);

        while (s.hasNext()) 
        {
            if (s.next().equals(string)) 
                i++;

        }

        return i;
    }
}

String tstr1是文本,String string是搜索到的键,现在我想统计搜索到的键在文本中出现了多少次(不区分大小写)。谢谢

java.lang.String 有一个方法 boolean equalsIgnoreCase(String anotherString),你应该在这里使用它。

最好在调用 .equals() 之前将字符串转换为小写或大写。这样做需要您对两个字符串进行完整操作,而 equalsIgnoreCase 可以在第一个不匹配时退出。

您要查找的是Stringclass

中的equalsIgnoreCase

public boolean equalsIgnoreCase(String anotherString)

Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

您也可以使用 toLowerCase()toUpperCase(),但我认为使用 equalsIgnoreCase() 更具可读性。

正如 Bathsheba 所指出的,这也更有效。这是因为一旦出现不匹配,equalsIgnoreCase() 将停止比较字符串。

关于此的更多信息 here. Read this

在这种情况下,我们使用 Pattern class 来实现所有大小写不区分大小写。 例如:

import java.util.regex.Pattern;

/**
*
* @author developerbhuwan
*/
public class HowToUseCaseInsensitive {

   public static void main(String[] args) {
       String searchKey = "cAt";
       String searchOn = "Cat is an animal";

      // In searchOn String is present but when
      // we use contains method of string then not found
      // due to case sensitive comparision
      if (searchOn.contains(searchKey)) {
          System.out.println("Cat found in searchIn String");
      } else {
          System.out.println("Cat not found in searchIn String");
      }

     // Output => Cat not found in searchIn String
     // To achieve case insensitive comparison
     // perfect solution for all case is by using
     // Pattern class as below
     if (Pattern.compile(Pattern.quote(searchKey),
        Pattern.CASE_INSENSITIVE).matcher(searchOn).find()) {
          System.out.println("Cat found in searchIn String");
    } else {
          System.out.println("Cat not found in searchIn String");
    }
   // Now Output => Cat found in searchIn String
  }
}

为了计算不区分大小写的段落中的单词,我们还使用了 Pattern class 和 while 循环:

例如:

public class CountWordsInParagraphCaseInsensitive {


    public static void main(String[] args) {
        StringBuilder paragraph = new StringBuilder();
        paragraph.append("I am at office right now.")
                .append("I love to work at oFFicE.")
                .append("My OFFICE located at center of kathmandu valley");
        String searchWord = "office";
        Pattern pattern = Pattern.compile(searchWord, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(paragraph);
        int count = 0;
        while (matcher.find())
            count++;
        System.out.println(count);

    }

}

试试这个:

if (string1.equalsIgnoreCase(string2)){
    Do.somthing 
}else{etc.}

注:方法equals()继承自Object,所以其签名不应该改变。