[英]Java Splitting a string within an array
I have a string array with username;password format such as 我有一个带用户名的字符串数组;密码格式如
login[] = { "jake;pass", "oliver;king" } etc...
I then have a for statement to set the password depending on the username 然后,我有一个for语句,用于根据用户名设置密码
for(int k = 0; k < login.length; k++){
if(login[k].??? == username){
password = ???
}
How can I use substring or split to verify the username before the ";" 如何使用子字符串或拆分来验证“;”之前的用户名 as well as the password after the ";" 以及“;”后面的密码 ? ? I want to set password = everything after the ; 我想设置密码=之后的所有内容; and login[k].substring/split to everything before the ; 和login [k] .substring / split到;之前的所有内容;
Don't try to condense too much. 不要试图凝结太多。 It'll end up working against you. 最终将对您不利。
Something like this should work: 这样的事情应该起作用:
for (int k = 0; k < login.length; k++) {
String[] split = login[k].split(";");
String user = split[0];
String pass = split[1];
if (user.equals(username)) { //make sure to use equals() for Objects
password = pass;
}
}
I really hope you're not actually storing plaintext passwords for anything that needs to be secure, though. 我真的希望您实际上并没有为任何需要保护的东西存储纯文本密码。
I don't think you can do it inline. 我认为您不能内联。 Maybe some stuff like this: 也许是这样的:
for(int k = 0; k < login.length; k++){
String[] s = login[k].split(";");
if(s[0].equals(username)){
password = s[1];
}
}
您可以使用从String拆分的方法,如下所示:
String[] parts = login[k].split(";");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.