How to compare only part of a String in Java [closed]

-2
String str = new String("Bruno Oliveira");
String str2 = new String("Gustavo Oliveira");

System.out.println(str.equals(str2)); //retorna false

How to compare only a certain part of a string?

    
asked by anonymous 24.07.2015 / 02:05

2 answers

2

I think you wanted something like this:

    String str = new String("Bruno Oliveira");
    String str2 = new String("Gustavo Oliveira");

    //cria array de strings usando o espaço como separador
    String[] arr = str2.split(" ");

    // busca na string alvo cada pedaço da string separada
    for (String s : arr) { 
        if (str.contains(s)) {
            System.out.println("match: " + s);
        }
    }
//retorno
//match: Oliveira
    
24.07.2015 / 02:33
0

I do not understand what you really need, but by the way I understand you want to find a substring within a string and compare if substring is contained in another string , if it is really this, you can use the contains method in conjunction with the substring of the second string . In the example below, I passed the initial indexes of substring . Perhaps not the best approach, as not all texts are fixed size, but it is just to exemplify.

    String s = new String("Bruno Oliveira");
    String s1 = new String("Gustavo Oliveira");
    System.out.println(s.contains(s1.substring(8,16)));
    
24.07.2015 / 02:33