在Java中有一种方法来检查条件:

"这个字符是否出现在字符串x中"

不使用循环?


当前回答

这是你要找的吗?

int index = string.indexOf(character);
return index != -1;

其他回答

string .contains()检查字符串是否包含指定的char值序列 string . indexof()返回字符串中第一次出现指定字符或子字符串的索引(此方法有4种变体)

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());
}
package com;
public class _index {

    public static void main(String[] args) {
        String s1="be proud to be an indian";
        char ch=s1.charAt(s1.indexOf('e'));
        int count = 0; 
        for(int i=0;i<s1.length();i++) {
            if(s1.charAt(i)=='e'){
                System.out.println("number of E:=="+ch);
                count++;
            }
        }
        System.out.println("Total count of E:=="+count);
    }
}

你可以使用string.indexOf('a')。

如果字符a出现在string中:

它返回中字符第一次出现的索引 对象表示的字符序列,或-1 性格不会出现。

如果有人用这个;

let text = "Hello world, welcome to the universe.";
let result = text.includes("world");
console.log(result) ....// true

结果将是true或false

这对我来说总是有效的