12

I have this code which searches a string array and returns the result if the input string matches the 1st characters of a string:

for (int i = 0; i < countryCode.length; i++) {
            if (textlength <= countryCode[i].length()) {
                if (etsearch
                        .getText()
                        .toString()
                        .equalsIgnoreCase(
                                (String) countryCode[i].subSequence(0,
                                        textlength))) {
                    text_sort.add(countryCode[i]);
                    image_sort.add(flag[i]);
                    condition_sort.add(condition[i]);
                }
            }
        }

But i want to get those string also where the input string matches not only in the first characters but also any where in the string? How to do this?

Reyjohn
  • 2,654
  • 9
  • 37
  • 63

7 Answers7

27

You have three way to search if an string contain substring or not:

String string = "Test, I am Adam";
// Anywhere in string
b = string.indexOf("I am") > 0;         // true if contains 

// Anywhere in string
b = string.matches("(?i).*i am.*");     // true if contains but ignore case

// Anywhere in string
b = string.contains("AA")  ;             // true if contains but ignore case
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
8

I have not enough 'reputation points' to reply in the comments, but there is an error in the accepted answer. indexOf() returns -1 when it cannot find the substring, so it should be:

    b = string.indexOf("I am") >= 0; 
Morris
  • 593
  • 7
  • 7
  • I think you should use string.contains("I am") because it is faster and shorter. You can find further information here (https://stackoverflow.com/questions/498686/is-string-contains-faster-than-string-indexof#:~:text=IndexOf(string)%20has%20no%20options,search%20using%20FindNLSString%20from%20kernel32.). – maxmitz Jan 21 '22 at 13:26
3

Check out the contains(CharSequence) method

compuguru
  • 1,035
  • 1
  • 10
  • 20
0

Try this-

etsearch.getText().toString().contains((String) countryCode[i]);
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
0

You could use contains. As in:

myString.contains("xxx");

See also: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html, specifically the contains area.

jsun1973
  • 31
  • 3
0

Use the following:

public boolean contains (CharSequence cs)

Since: API Level 1

Determines if this String contains the sequence of characters in the CharSequence passed.

Parameters

cs the character sequence to search for.

Returns

true if the sequence of characters are contained in this string, otherwise false

GingerHead
  • 8,130
  • 15
  • 59
  • 93
0

Actually, the default Arrayadapter class in android only seems to search from the beginning of whole words but by using the custom Arrayadapter class, you can search for parts of the arbitrary string. I have created the whole custom class and released the library. It is very easy to use. You can check the implementation and usage from here or by clicking on the link provided below.

https://github.com/mohitjha727/Advanced-Search-Filter

You can refer to this simple example- We have names " Mohit " and "Rohan" and if We put " M" only then Mohit shows up in search result, but when we put "oh" then both Mohit and Rohan show up as they have the common letter 'oh'.

Thank You.