没有空格的字符串

时间:2018-06-15 11:05:46

标签: java android

所以基本上我正在为我的Android应用程序创建一个测验。但是我注意到当用户键入正确的答案时,#34;愤怒"并且旁边有一个空格,答案显示为错误。我想知道我怎么能改变这个,以便答案后的空格不包括在内?谢谢!

public void question3(View view) {

        // Get the typed answer by the user and convert it to lower case.
        EditText question3 = (EditText) findViewById(R.id.question3_typed); 

        String question3_answer = question3.getText().toString().toLowerCase();

        if (question3_answer.equals("angry")) {
            answer3 = "correct";
        } else answer3 = "wrong";

1 个答案:

答案 0 :(得分:0)

你应该使用String类的trim方法来删除字符串开头或结尾的任何空格。像这样修改你的代码

public void question3(View view) {

    // Get the typed answer by the user and convert it to lower case.
    EditText question3 = (EditText) findViewById(R.id.question3_typed); 

    String question3_answer = question3.getText().toString().trim().toLowerCase();

    if (question3_answer.equals("angry")) {
        answer3 = "correct";
    } else answer3 = "wrong";