【问题标题】:Make the string case insensitive in java在java中使字符串不区分大小写
【发布时间】:2015-09-08 13:52:31
【问题描述】:

我的应用程序中有一个问题我有一个String,其中包含一个搜索词,然后我使用这个词从一个长文本中搜索,我希望所有包含搜索词的词都返回(例如 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 是搜索键,现在我想计算搜索键在文本中出现的次数(不区分大小写)。谢谢

【问题讨论】:

  • 你能贴出你试过的代码吗?
  • 您在寻找equalsIgnoreCase() 还是toLower() / toUpper()。如果不是,请进一步说明您的问题
  • 是的,我喜欢使用 equalsIgnoreCase() 或 toLower() / toUpper(),例如,如果我的搜索词是 word 并且在文本中是 words 我想找到它.

标签: java string search case-sensitive case-insensitive


【解决方案1】:

您正在寻找的是equalsIgnoreCaseStringclass

public boolean equalsIgnoreCase(String anotherString)

将此字符串与另一个字符串进行比较,忽略大小写。如果两个字符串的长度相同,并且两个字符串中的对应字符相等,忽略大小写,则认为两个字符串相等。

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

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

更多关于这个here。也请阅读this

【讨论】:

  • ...更高效。
  • @Bathsheba 这也是真的。我会将其添加到答案中。谢谢!
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    在这个场景中,我们使用Pattern 类来实现所有大小写都不区分大小写。 例如:

    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
      }
    }
    

    【讨论】:

    • 谢谢你的回答,我更新了我的问题,你能告诉我如何实现这个。计算searchKeysearchOn 中的次数。谢谢!!!
    【解决方案4】:

    为了计算段落中不区分大小写的单词,我们还使用了 Pattern 类和 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);
    
        }
    
    }
    

    【讨论】:

      【解决方案5】:

      试试这个:

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

      注意: 方法equals() 继承自Object,因此不应更改其签名。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-22
        • 2016-01-26
        • 2014-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多