How to check if part of a string is equal to another string in android

How to check if part, if one line is equal to another? For example, if I have one line with the value "hello" and a line with the value "he", how can I compare them to check that "hello" contains "he".

If this has not been explained very well, tell me and I will try to clear it.

+7
source share
5 answers

"Hello".toLowerCase().contains("He".toLowercase()); same as java using String class contains () .

+11
source

There is a contains String method:

 String str = "Hello"; if (str.toLowerCase().contains("he")) // ... 

There is also a startsWith method on a String :

 if (str.toLowerCase().startsWith("he")) // ... 

If both lines are variables with unknown contents, and the case is not important, then:

 str.toLowerCase().contains(str2.toLowerCase())) str.toLowerCase().startsWith(str2.toLowerCase())) 
+4
source

Try String.contains (). Documents can be found here.

+3
source

If you are unsure of the case of strings, convert both to lowercase:

 if("Hello".toLowerCase().indexOf("he".toLowerCase()) >= 0) 
+3
source
 string string1 = "yellow"; string string2 = "Hello"; boolean contains = false; int length = string1.length(); for (int i=0; i < length; i++) { for (int j=i+1; j < length; j++) { // Don't go beyond last letter for second part of substring if (j < length - 1) { string temp = string1.substring(i, j); contains == string2.contains(temp); } } } 
+1
source

All Articles