String中的regoinmatchs方法是怎么回事??

来源:百度知道 编辑:UC知道 时间:2024/06/06 20:04:25
class A
{ public static void main(String arg[])
{ int number=0;
String s="student;entropy;engage,english,client";
//这和s="how are you"一样么,里边的分号和逗号没用吧?
for(int k=0;k<s.length;k++)
{ if(s.regionMatchs(k,"en",0,2))
//s.regionMatchs(k,"en",0,2)是什么意思??
{ number++;}
}
System.out.println("number="+number);
}
}

找出str中包含“en”的个数

建议多看看j2se5.0API:
public boolean regionMatches(int toffset,
String other,
int ooffset,
int len)测试两个字符串区域是否相等。
该 String 对象的一个子字符串与参数 other 的一个子字符串进行比较。如果这两个子字符串表示相同的字符序列,则结果为 true。要比较的 String 对象的子字符串从索引 toffset 处开始,长度为 len。要比较的 other 的子字符串从索引 ooffset 处开始,长度为 len。当且仅当下列至少一项为 true 时,结果才为 false :

toffset 为负。
ooffset 为负。
toffset+len 大于此 String 对象的长度。
ooffset+len 大于另一个参数的长度。
存在某个小于 len 的非负整数 k,它满足:this.charAt(toffset+k) != other.charAt(ooffset+k)

参数:
toffset - 字符串中子区域的起始偏移量。
other - 字符串参数。
ooffset - 字符串参数中子区域的起始偏移量。
len - 要比较的字符数。
返回:
如果字符串的指定子区域完全匹配字符串参数的指定子区域,则返回 true;否则返回 false。

public static void main(String[] args) {
int number = 0;
String str = "student;entropy;engage,english,client";
System.out.println(str.length());//打印37,说明引号内的标点是占位的

String howStr = "how are you";
System.out.