Java string match issue

I ran into a very simple problem. For some time, little things can take all day :( but thank the stackoverflow staff who are always trying to help :)

I am trying to match 2 strings if they match and should return TRUE

now i use this

if (var1.indexOf(var2) >= 0) {
return true;
}

But if var1 is set to support, and var2 is set to inta or ain, etc., it still returns true :(. Is there any way in java that can do full text match, not partial? For example

if("mango"=="mango"){
return true;
}

thank!!

+3
source share
4 answers

Why not just use the built-in String method equals()?

return var1.equals(var2);
+5
source
if( "mango".equals("mango") ) { 
   return true;
}

, == Java, , .

+4

equals equalsIgnoreCase java.util.String . null , , commons StringUtils , .

+3

, , == , :

String s1 = new String("something");
String s2 = new String("something");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

, s1 s2 , . , false true.

+1

Source: https://habr.com/ru/post/1733992/


All Articles