在Java中有一种方法来检查条件:
"这个字符是否出现在字符串x中"
不使用循环?
在Java中有一种方法来检查条件:
"这个字符是否出现在字符串x中"
不使用循环?
当前回答
如果有人用这个;
let text = "Hello world, welcome to the universe.";
let result = text.includes("world");
console.log(result) ....// true
结果将是true或false
这对我来说总是有效的
其他回答
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 .contains()检查字符串是否包含指定的char值序列 string . indexof()返回字符串中第一次出现指定字符或子字符串的索引(此方法有4种变体)
you can use this code. It will check the char is present or not. If it is present then the return value is >= 0 otherwise it's -1. Here I am printing alphabets that is not present in the input.
import java.util.Scanner;
public class Test {
public static void letters()
{
System.out.println("Enter input char");
Scanner sc = new Scanner(System.in);
String input = sc.next();
System.out.println("Output : ");
for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
if(input.toUpperCase().indexOf(alphabet) < 0)
System.out.print(alphabet + " ");
}
}
public static void main(String[] args) {
letters();
}
}
//Ouput Example
Enter input char
nandu
Output :
B C E F G H I J K L M O P Q R S T V W X Y Z
你可以使用string.indexOf('a')。
如果字符a出现在string中:
它返回中字符第一次出现的索引 对象表示的字符序列,或-1 性格不会出现。
是的,在字符串类上使用indexOf()方法。请参阅此方法的API文档