简单的方法如下,直接调用String的查找字串方法equals():public class MyTest1 { public static void main(String[] args) { String str[] = {"abcdefgbc","bvc","ab"}; String substr = "bc"; for(int i=0;i<str.length;i++) if(str[i].equals(substr)){ System.out.println("存在,且位于数组第"+(i+1)+"个位置"); break; } else if(i==str.length-1)System.out.println("不存在"); }}
public class Test{public static int compareStr(String str,String x[]){ int suffix=0;//记录字符串数组的下标 for(int i=0;i<x.length;i++){ if(str==x[i]) suffix=i+1; } return suffix;}public static void main(String[]agrs ){ String [] ken={"kin","zuo","kin","hen","long","ah","king","aj "}; if(Test.compareStr("king",ken)!=0) System.out.println(Test.compareStr("king",ken)); else System.out.println("不存在匹配");}}
没看懂~~不知道是不是这样的~~~public static int compareStr(String str,String x[]){ int suffix=0;//记录字符串数组的下标 for(int i=0;i<x.length;i++){ if(str.equals(x[i])) suffix=i; } return suffix;}