//需求:求子串在主串中第一次出现的位置问题
class StringGetIndex
{
public static int index(String str,String tes,int pos)
{
if(pos>0)
{
int n = str.length();
int m = tes.length();
int i = pos;
while(i<=n-m+1)
{
String subString = str.substring(i,i+m);
if(subString.compareTo(tes)==0)
{
return i;
}
else
i++;
}
}
return 0;
}
public static void main(String[] args)
{
String str = "fghijklmnoabcdeabcdepqrstuvwxyz";
String tes = "abcde";
int pos=1;
int i = index(str,tes,pos);
System.out.println(str.length()+" "+tes.length()+"\n"+i);
}
}
子串问题
最新推荐文章于 2021-10-25 10:10:25 发布