gpt4 book ai didi

android - 字符串值看起来相同但彼此不是 ".equals()"

转载 作者:搜寻专家 更新时间:2023-11-01 08:03:21 24 4
gpt4 key购买 nike

我正在制作一个登录屏幕。它不起作用,所以我决定显示密码(保存到文件中),并将输入的密码显示在屏幕上以进行比较。

// filePassword is the password inputted from the file as a String.
// password is the String value of the EditText on the screen.
if (password.equals(filePassword))
{
logIn();
}

我这样比较 Strings:

else 
{
scr.setText("." + filePassword + "." + '\n' + "." + password + ".");
}

这是 String 比较:

enter image description here

我这样比较 chars

else 
{
char[] filePass = filePassword.toCharArray();
char[] pass = password.toCharArray();
}

这是 char 比较:

enter image description here

每次按下按钮时,此 char 数组 的输出都会改变。

显然 char 数组 存在差异。我不知道为什么。这可能与我处理文件的方式有关吗?可能不会,考虑到它在我项目的其他任何地方都没有发生。

有什么办法可以解决这个比较问题吗?对两个值都使用 .toString() 方法不起作用。

______________________________________________________________________________________________________________________________

解决方案

if (password.equals(filePassword))

需要:

if (password.equals(filePassword.trim()))

char array 比较总是不同的。比较它们的方法是这样的:

import java.util.Arrays; // add to top of class
scr.setText(Arrays.toString(password.toCharArray()));
scr.append(Arrays.toString(filePassword.toCharArray()));

最佳答案

在任何字符串中是否可以有一个额外的字符?也许回车 \r?您可以对两者都使用 length(),或者使用 trim(),然后再进行比较。或者尝试:

System.out.println(Arrays.toString(password.toCharArray()));  
System.out.println(Arrays.toString(filePassword.toCharArray()));

关于android - 字符串值看起来相同但彼此不是 ".equals()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17929290/

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