Android Studio - Doubt over String Comparisons

1

I have the following code

static String q1 = "select * from faculdade";

if(edit.getText().toString().equals(q1)) {

    bar.setCurrentPosition(++position);
    Toast.makeText(getApplicationContext(), "Certa Resposta!", Toast.LENGTH_LONG).show();

}

When I say the way it was declared in the string and hit the button, it executes what was requested.

However, if the person gives more than 2 spaces or put a comma together or separated, he does not recognize. Only works if I type the same as the variable. How do I get around this? Thanks to anyone who can help.

    
asked by anonymous 25.01.2018 / 04:19

1 answer

1

You can compare using the method:

  • .trim() , to remove spaces

    import java.io.*; 
    public class Test {
    
       public static void main(String args[]) {
              String Str = new String("Hello World");
              System.out.println(Str.trim() ); 
    
           }
        }
    

    printa: HelloWorld

  • .replaceAll("qualquer caracter aqui", "será substituido pelo carácter que estiver aqui")

  • 25.01.2018 / 04:48