gpt4 book ai didi

java - 没有空格的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:04 24 4
gpt4 key购买 nike

所以基本上我正在为我的 Android 应用程序创建一个测验。但是我注意到,当用户输入“愤怒”的正确答案并且旁边有一个空格时,答案会显示为错误。我想知道如何更改此设置,以便不包含答案后的空格?谢谢!

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";

最佳答案

您应该使用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";

关于java - 没有空格的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50874309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com