Android Studio - Doubt about String’s comparisons

Asked

Viewed 80 times

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 as stated in the string and press the button, it executes what was requested.

However, if one gives more than 2 spaces or puts a comma together or separated, he does not recognize. It only works if I type much like the variable. How do I get around it ? Thanks to anyone who can help.

1 answer

1

You can compare using the method:

  1. .trim(), to clear 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

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

  • Could you for an example of how I would put ? I have never used Trim ()

  • @Lucianafreireefelipecorreia use: edit.getText().toString().trim().equals(q1) ;)

  • I came back to thank people. thank you very much. replaceAll worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.