public class MyString {
private char[] value;
private int count;
private int offset;
public boolean startsWith(MyString prefix , int toffset) {
//将原串进行表示
char[] tv = value;
int to = offset + toffset;
//讲被比较串进行表示
char[] pv = value;
int po = prefix.offset;
int pc = prefix.count;
if((toffset < 0) || (toffset > count - pc))
return false;
while(--pc >= 0) {
if(tv[to++] != pv[po++])
return false;
}
return true;
}
public static void main(String[] args) {
MyString ms = new MyString();
ms.value = new char[]{'a','b','c','d','e'};
MyString msp = new MyString();
msp.value = new char[]{'a','b','c'};
boolean b = ms.startsWith(msp,0);
System.out.println(b);
}
}
本文深入探讨了MyString类中startsWith方法的实现细节,详细解释了如何通过字符数组比较来判断字符串是否以特定前缀开头。通过实例演示,读者可以更好地理解字符串操作的基础知识。
962

被折叠的 条评论
为什么被折叠?



