How to check if a String contains another String in a case insensitive manner in Java?

前端 未结 19 1421
渐次进展
渐次进展 2020-11-22 03:20

Say I have two strings,

String s1 = \"AbBaCca\";
String s2 = \"bac\";

I want to perform a check returning that s2 is contained

相关标签:
19条回答
  • 2020-11-22 03:59

    Yes, this is achievable:

    String s1 = "abBaCca";
    String s2 = "bac";
    
    String s1Lower = s1;
    
    //s1Lower is exact same string, now convert it to lowercase, I left the s1 intact for print purposes if needed
    
    s1Lower = s1Lower.toLowerCase();
    
    String trueStatement = "FALSE!";
    if (s1Lower.contains(s2)) {
    
        //THIS statement will be TRUE
        trueStatement = "TRUE!"
    }
    
    return trueStatement;
    

    This code will return the String "TRUE!" as it found that your characters were contained.

    0 讨论(0)
  • 2020-11-22 04:00

    Yes, contains is case sensitive. You can use java.util.regex.Pattern with the CASE_INSENSITIVE flag for case insensitive matching:

    Pattern.compile(Pattern.quote(wantedStr), Pattern.CASE_INSENSITIVE).matcher(source).find();
    

    EDIT: If s2 contains regex special characters (of which there are many) it's important to quote it first. I've corrected my answer since it is the first one people will see, but vote up Matt Quail's since he pointed this out.

    0 讨论(0)
  • 2020-11-22 04:01

    There is a simple concise way, using regex flag (case insensitive {i}):

     String s1 = "hello abc efg";
     String s2 = "ABC";
     s1.matches(".*(?i)"+s2+".*");
    
    /*
     * .*  denotes every character except line break
     * (?i) denotes case insensitivity flag enabled for s2 (String)
     * */
    
    0 讨论(0)
  • 2020-11-22 04:02
    String x="abCd";
    System.out.println(Pattern.compile("c",Pattern.CASE_INSENSITIVE).matcher(x).find());
    
    0 讨论(0)
  • 2020-11-22 04:03

    I did a test finding a case-insensitive match of a string. I have a Vector of 150,000 objects all with a String as one field and wanted to find the subset which matched a string. I tried three methods:

    1. Convert all to lower case

      for (SongInformation song: songs) {
          if (song.artist.toLowerCase().indexOf(pattern.toLowercase() > -1) {
                  ...
          }
      }
      
    2. Use the String matches() method

      for (SongInformation song: songs) {
          if (song.artist.matches("(?i).*" + pattern + ".*")) {
          ...
          }
      }
      
    3. Use regular expressions

      Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
      Matcher m = p.matcher("");
      for (SongInformation song: songs) {
          m.reset(song.artist);
          if (m.find()) {
          ...
          }
      }
      

    Timing results are:

    • No attempted match: 20 msecs

    • To lower match: 182 msecs

    • String matches: 278 msecs

    • Regular expression: 65 msecs

    The regular expression looks to be the fastest for this use case.

    0 讨论(0)
  • 2020-11-22 04:05

    You can use regular expressions, and it works:

    boolean found = s1.matches("(?i).*" + s2+ ".*");
    
    0 讨论(0)
提交回复
热议问题