How do I compare two words from two different chains?


I just want to compare the first word of two strings if they do not matter or not ...


String1="No puedo comparar"
String2="Si puedo comparar"

I would have to give false since the SI and the NO are not equal.

String1="Si puedo comparar"
String2="Si podre comparar"

I would have to give true since the Si and the SI are equal.

You have to ask for the value of the string by keyboard, then this is the part of the code that I have, but when I execute it, it always gives me that they are the same, so write different things, what's wrong?:

import java.util.Scanner;
public class comparacion{
    String cadena1=("");
    String cadena2=("");

    String cadena1Array[]=cadena1.split(" ");
    String c=cadena1Array[0];
    String cadena2Array[]=cadena2.split(" ");
    String c2=cadena1Array[0];

import java.util.Scanner;

public class main {

    public static void main(String[] args) {

        comparacion q=new comparacion();

        System.out.println("Ingrese primera frase");
        //llamado a la clase comparacion
        Scanner read=new Scanner(;
        //Metodo de lectura de la variable cadebna 1 

        System.out.println("Ingrese segunda frase");
        Scanner read2=new Scanner(;

        //Quitar equals y comparar dos palabras iguales.
asked by Diego Alejandro 06.10.2016 в 03:50

5 answers


To separate the first word of your string you could use the function split() of javascript, then check if a string exists in both words.

To check that a string exists in another you can use the function contains() of javascript.

That way you would have a script like the following:

var a = "Si puedo comparar";
var b = "Si podre comparar";

var c = a.split(" ");
var d = b.split(" ");

if(c[0].includes("Si") && d[0].includes("Si")){
  console.log(c[0]+" "+d[0]);
  console.log("Bien palabras son iguales");
  console.log(c[0]+" "+d[0]);
  console.log("Mal palabras son diferentes");
answered by 06.10.2016 в 04:05

You could use the split and then compare with position 0 which would be the first word before a blank space in a string

   Scanner scan = new Scanner(;
   System.out.println("Primera Frase");
   String cadena1=;
   System.out.println("Segunda Frase");
   String cadena2=;

   /* Teniendo en Cuenta Mayusculas ni Minusculas */
   System.out.println(cadena1.split(" ")[0].equals(cadena2.split(" ")[0]));

  /* Sin tener en cuenta Mayusculas ni Minusculas */
   System.out.println(cadena1.split(" ")
    [0].toLowerCase().equals(cadena2.split(" ")[0].toLowerCase()));
answered by 06.10.2016 в 05:23

The previous answers are already correct, I show you another way of doing it although I always use the function split () (it's faster, harder & stronger);

StringTokenizer st = new StringTokenizer(TuString, el delimitador);
 //StringTokenizer st = new StringTokenizer("hola que pasa", " ");

   while(st.hasMoreTokens() && "condicion booleana si quieres romper el bucle") {
       String primerPalabra= st.nextToken();
       if(primeraPalabra.equals("lo que sea");

and I recommend you take a look at Apache StringUtils link

answered by 06.10.2016 в 08:16

try it like that

"hola mundo"=="hola mundo"

to compare the binary operator "==" is used eye: the assignment operator is "=" do not confuse

answered by 06.10.2016 в 04:02

To do this you can do with a normal comparison within an if:

if(String1 == String2){
    //Sentencia a ejecutar 

This would give a comparison between the two variables but if in case the variables would be:


String1="If I can compare";



In this case, the Strings would be different and I would not do the comparison, to ensure that the comparisons are made correctly, the methods can be used:


toUpperCase () - > Convert the string to uppercase


toLowerCase () - > Convert the string to lowercase

Now the comparison would be like this:

if(String1.toUpperCase() == String2.toUpperCase()){
        //Sentencia a ejecutar 

But there is another case, this is when your variables are the following:





There the values are the words are the same but with the spaces and the string takes different value, for this case the following method would be used:


trim () -> Remove spaces at the beginning and at the end

The comparison is as follows:

if(String1.trim().toUpperCase() == String2.trim().toUpperCase()){
      //Sentencia a ejecutar 
answered by 06.10.2016 в 04:19