Android - 如何检查另一个以逗号分隔的字符串中是否存在字符串值。
String all_vals = "617,618,456,1,234,5,5678,225";
String check_val= "456";
如何检查,
if (all_vals contains check_val) {
}
答案 0 :(得分:8)
将逗号分隔的字符串转换为包含split
的数组,然后将其转换为List
Arrays.asList
,然后使用contains
。
String all_vals = "617,618,456,1,234,5,5678,225";
List<String> list = Arrays.asList(all_vals.split(","));
if (list.contains(check_val)) {
}
这样可以防止误报只是检查字符串中是否存在contains
直接在字符串all_vals
上的子字符串,例如all_vals.contains("4")
会在直接true
案例中返回String#contains
。
答案 1 :(得分:1)
String all_vals = "617,618,456,1,234,5,5678,225";
String check_val= "5678";
int place = 1;
String[] strings = all_vals.split(",");
for (String str : strings) {
if(str.equals(check_val))
{
System.out.println("We have string in all_val on place: " + place);
}
place++;
}
答案 2 :(得分:0)
$(".class").on('change', function(){
var id = $(this).attr("id");
$("#"+id).closest("tr").toggleClass("checked", this.checked);
});
答案 3 :(得分:0)
Java 8:
String all_vals = "617,618,456,1,234,5,5678,225";
String check_val= "5678";
Arrays.stream(all_vals.split(",")).anyMatch(check_val:: equals)
if(Arrays.stream(all_vals.split(",")).anyMatch(check_val:: equals)){
System.out.println("The value is present");
}
答案 4 :(得分:0)
Ref :: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)
public boolean contains(CharSequence s)
仅当此字符串包含指定的char值序列时才返回true。
Ex
/******************************************************************************
public boolean contains(CharSequence s)
*******************************************************************************/
public class Main
{
public static void main(String[] args) {
String all_vals = "617,618,456,1,234,5,5678,225";
String check_val= "456";
System.out.println("String contains :: "+all_vals.contains(check_val));
}
}