Skip to main content

Is the Contains Method in java.lang.String Case-sensitive?


Say I have 2 strings,




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



I want to preform a check returning that s2 is contained within s1. I can do this with:




return s1.contains(s2);



I am pretty sure that contains() is case sensitive, however I can't determine this for sure from reading the documentation. If it is then I suppose my best method would be something like:




return s1.toLowerCase().contains(s2.toLowerCase());



All this aside, does anyone know of another (possibly better) way to accomplish this without caring about case-sensitivity?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. 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(s2), Pattern.CASE_INSENSITIVE).matcher(s1).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.

    ReplyDelete
  2. One problem with the answer by Dave L. is when s2 contains regex markup such as \d etc.

    You want to call Pattern.quote() on s2:

    Pattern.compile(Pattern.quote(s2), Pattern.CASE_INSENSITIVE).matcher(s1).find();

    ReplyDelete
  3. Yes this is achievable, I just did it for a school assignment actually.

    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();

    if (s1Lower.contains(s2)) {

    //THIS statement will be TRUE
    String trueStatement = "TRUE!"
    }

    return trueStatement;


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

    ReplyDelete
  4. DrJava would be an extremely easy way to test this when the documentation fails you. Just type a couple of test cases into its Interactions window, and you should find out.

    ReplyDelete
  5. You can use

    org.apache.commons.lang3.StringUtils.containsIgnoreCase("AbBaCca", "bac");


    apache commons lib is very useful for this sort of things. And this particular one may be better than regular expressions as regex is always expensive in terms of performance.

    ReplyDelete
  6. I'm not sure what your main question is here, but yes, .contains is case sensitive.

    ReplyDelete
  7. It will look for the exact sequence of characters that are passed to it.

    If you want to ignore case then what you have there looks fine to me.

    ReplyDelete
  8. String x="abCd";
    System.out.println(Pattern.compile("c",Pattern.CASE_INSENSITIVE).matcher(x).find());

    ReplyDelete
  9. A simpler way of doing this (without worrying about pattern matching) would be converting both Strings to lowercase:

    String foobar = "fooBar";
    String bar = "FOO";
    if (foobar.toLowerCase().contains(bar.toLowerCase()) {
    System.out.println("It's a match!");
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.