2

Can we use "contains" and "equalignorecase" functions of string at same time.

i had a data in which i need to search a string "NBN", if found i need to update a flag. But im seeing there are

"nBN","nBn","NBn","nbN","NbN","Nbn"

also existing in the set of data . so i'm getting multiple combinations and comparisons.

Is there any way to overcome these many comparisons by using both functions at a time ?

2
  • 3
    looks like you're looking for something like a String.containsIgnoreCase(String) method. Is this correct? Commented Oct 9, 2013 at 4:58
  • 2
    x.toUpperCase().contains(y.toUpperCase()) (beware of the potential NPEs) Commented Oct 9, 2013 at 4:59

5 Answers 5

5

You can use Apache StringUtils#containsIgnoreCase()

 StringUtils.containsIgnoreCase("WholeString", "NBn");
Sign up to request clarification or add additional context in comments.

1 Comment

Find the source here commons.apache.org/proper/commons-lang/apidocs/src-html/org/… and here commons.apache.org/proper/commons-lang/javadocs/api-3.0/… if you don't want to include commons just for this.
4

Think you might find it easier to use String#toLowerCase instead of String#equalsIgnoreCase

For example...

if ("I want my NBN".toLowerCase().contains("nbn")) {...}

2 Comments

That makes a nice change :P
@user1835935 Can you please mark it as answer, Since your problem solved ??
2

While there is no built in functionality for this directly, best practice is to convert both Strings to lower case then use contains()

String matchString = "NPN";
String lowercaseMatchString = matchString.toLowerCase();
String lowercase = stringToTest.toLowerCase();

return lowercaseMatchString.contains(lowercase);

4 Comments

if(a) { return true; }, please use return a;
Not necessarily, my code allows for further testing if false. Your's returns immediately. If I had a ? return true : return false, I would use return a
You edit make my comment void, for sure, but why did you add the "reverse" contains is kind of a mystery..
Edited to make more concise and use your input
0

From Apache Commons
http://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/StringUtils.html:

1337    public static boolean containsIgnoreCase(final CharSequence str, final CharSequence searchStr) {
1338        if (str == null || searchStr == null) {
1339            return false;
1340        }
1341        final int len = searchStr.length();
1342        final int max = str.length() - len;
1343        for (int i = 0; i <= max; i++) {
1344            if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, len)) {
1345                return true;
1346            }
1347        }
1348        return false;
1349    }

http://commons.apache.org/proper/commons-lang/javadocs/api-3.0/src-html/org/apache/commons/lang3/CharSequenceUtils.html

187        static boolean regionMatches(CharSequence cs, boolean ignoreCase, int thisStart,
188                CharSequence substring, int start, int length)    {
189            if (cs instanceof String && substring instanceof String) {
190                return ((String) cs).regionMatches(ignoreCase, thisStart, ((String) substring), start, length);
191            } else {
192                // TODO: Implement rather than convert to String
193                return cs.toString().regionMatches(ignoreCase, thisStart, substring.toString(), start, length);
194            }
195        }

2 Comments

This really should be included in your previous answer - IMHO
@MadProgrammer, I'd sooner delete it than include with the other answer. I think this method is unnecessarily long and is simply to offer an equivalent alternative to including Commons.
0

If you just want to check the wether your data contains "NBN" then StringUtils.containsIgnoreCase() is the best option as it serves the exact purpose. But if you also want to count the occurrences or anything else then writing a custom solution will be a better option.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.