“a*”: 以字符’a’开头的字符串
“*bc*”: 包含字符串’bc’的字符串
“A??”: 以字符’A’开头,且长度为3的字符串
“?A?”: 以字符’A’居其间,且长度为3的字符串
class MyString{
String str;
//其它方法或成员定义
//构造函数
public Boolean isLike(String regex){
……
}
}
new MyString(“abcd”).isLike(“a*”)--true
new MyString(“ABCD”).isLike(“*C*”)--true
new MyString(“ABCD”).isLike(“ABC?”)--true
编写isLike()方法,用以判断字符串是否匹配。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- haha662 2010-07-14 10:04关注
public class MyString {
private String str;MyString(String str){ this.str=str; } public boolean isLike(String regex){ String re=regex; re=re.replaceAll("[*]", "[a-zA-Z0-9]*"); re=re.replaceAll("[?]", "."); Pattern p=Pattern.compile(re); Matcher m=p.matcher(str); return m.matches(); }
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报