Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从字符串中的另一个字符串中查找任何字符首次出现的索引?_Java - Fatal编程技术网

Java 如何从字符串中的另一个字符串中查找任何字符首次出现的索引?

Java 如何从字符串中的另一个字符串中查找任何字符首次出现的索引?,java,Java,上面是说明,下面是我的代码 /* returns the index of the first occurrence of any of the * characters in chars in String s or -1 if none of the characters * in chars are found in s. */ publicstaticvoidmain(字符串[]args){ 猥亵(“卡邦巴”、“炸弹”); } 公共静态int索引(字符串s、字符串字符){ i

上面是说明,下面是我的代码

/* returns the index of the first occurrence of any of the
 * characters in chars in String s or -1 if none of the characters
 * in chars are found in s. */
publicstaticvoidmain(字符串[]args){
猥亵(“卡邦巴”、“炸弹”);
}   
公共静态int索引(字符串s、字符串字符){
int指数=0;
字符串rev=“”;
对于(int i=s.length()-1;i>=0;i--){
rev+=s.charAt(i);
}
字符串x=rev;

对于(int i=0;i),可以使用StringUtils类来防止循环迭代。 或


堆栈溢出不是一项免费的家庭作业服务,不管你听到什么或别人告诉你什么。你需要告诉我们问题出在你的代码中,或者至少是你对问题的最佳估计。我想当你找到匹配项时,你想打破循环,所以在
index=I;
添加
break;
之后,请阅读-summary是,这不是一种理想的方式来称呼志愿者,并且可能会对获得答案产生反作用。请不要将此添加到您的问题中。是的,我的错误是,它是indexOf。在StringUtils类下,有更多的indexOf方法,因为我们可以一次获得多个字符的位置。
public static void main(String[] args){

     indexOfAny("kabomba","bomb");

}   

public static int indexOfAny(String s, String chars) {

        int index = 0;
        String rev = "";

        for(int i = s.length()-1;i>=0;i--){
            rev+=s.charAt(i);
        }

        String x = rev;

        for(int i = 0;i<x.length();i++){

            for(int j = 0; j<chars.length();j++){
                if(x.charAt(i)==chars.charAt(j)){
                    index = i;//**
                }else {
                    index = -1;
                }
            }
        }
        System.out.println(index);
        return index;
    }