在Java中有一种方法来检查条件:
"这个字符是否出现在字符串x中"
不使用循环?
在Java中有一种方法来检查条件:
"这个字符是否出现在字符串x中"
不使用循环?
当前回答
这是你要找的吗?
int index = string.indexOf(character);
return index != -1;
其他回答
如果有人用这个;
let text = "Hello world, welcome to the universe.";
let result = text.includes("world");
console.log(result) ....// true
结果将是true或false
这对我来说总是有效的
static String removeOccurences(String a, String b)
{
StringBuilder s2 = new StringBuilder(a);
for(int i=0;i<b.length();i++){
char ch = b.charAt(i);
System.out.println(ch+" first index"+a.indexOf(ch));
int lastind = a.lastIndexOf(ch);
for(int k=new String(s2).indexOf(ch);k > 0;k=new String(s2).indexOf(ch)){
if(s2.charAt(k) == ch){
s2.deleteCharAt(k);
System.out.println("val of s2 : "+s2.toString());
}
}
}
System.out.println(s1.toString());
return (s1.toString());
}
string .contains()检查字符串是否包含指定的char值序列 string . indexof()返回字符串中第一次出现指定字符或子字符串的索引(此方法有4种变体)
String temp = "abcdefghi";
if(temp.indexOf("b")!=-1)
{
System.out.println("there is 'b' in temp string");
}
else
{
System.out.println("there is no 'b' in temp string");
}
您可以使用String类中的两个方法。
string .contains()检查字符串是否包含指定的char值序列 string . indexof()返回字符串中第一次出现指定字符或子字符串的索引,如果没有找到该字符则返回-1(此方法有4种变体)
方法1:
String myString = "foobar";
if (myString.contains("x") {
// Do something.
}
方法2:
String myString = "foobar";
if (myString.indexOf("x") >= 0 {
// Do something.
}
链接作者:Zach Scrivena