[英]Android if statement - if a string contains any of the strings from an array
Can you give me a hand please. 你能帮我一下吗。
I need to do a check, to check if the userCode matches any code in my string array or if the oldUser code also matches any code from the array 我需要进行检查,以检查userCode是否匹配我的字符串数组中的任何代码,或者oldUser代码是否也匹配数组中的任何代码
String[] code = {"1111", "2222", "3333", "4444", "5555"};
String userCode;
String oldUser;
if (userCode.equals(code) || oldUser.equals(code)) {
RUN
} else { die }
thanks 谢谢
To achieve a simple verification you can loop through your array OR Easy way to do it 要实现简单的验证,您可以遍历数组或简单的方法
List< String > code = Arrays.asList( new String[] { "1111", "2222", "3333", "4444", "5555" } );
String userCode;
String oldUser;
if (code.contains(userCode) || code.contains(oldUser)) {
//doStuff()
} else {
return;
}
尝试这个
boolean contains = Arrays.asList(arr).contains(str);
Loop the array and check each entry with your user values 循环数组,并使用用户值检查每个条目
String[] code = {"1111", "2222", "3333", "4444", "5555"};
for (int i = 0; i < code.length; i++) {
if (userCode.equals(code[i]) || oldUser.equals(code[i])) {
//do
}
}
Or using Arrays.asList()
, 或使用Arrays.asList()
,
List<String> codes = Arrays.asList("1111", "2222", "3333", "4444", "5555");
if (codes.contains(userCode) || codes.contains(oldUser)) {
//do
}
Use a foreach with a boolean var 在布尔变量中使用foreach
boolean isTheSame;
For(String myArrayCodes; code;) {
if (userCode.equals(myArrayCodes) || oldUser.equals(myArrayCodes))
isTheSame = true;
break;
}
}
if (isTheSame) { // usercode or oldUser is on array
doSomething();
}
else {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.