关于String的方法contains()
contains()可以不可以传一个正则表达式进去?
如果不能的话,String有什么方法可以去匹配某一个String里面是否包含某个规则的字符串?
------解决方案--------------------String的matches是匹配整个,如果要一部分,可以按下面这样
import java.util.regex.*;
public class Test67 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] as = { "Driver\\A ", "whatever1 ", "aaDriver\\B ", "whatever2 " };
Pattern p = Pattern.compile( "Driver\\\\[A-Z] ");
Matcher m;
for (String s : as) {
m = p.matcher(s);
if (m.find())
System.out.println(s);
}
}
}
------解决方案--------------------matchers 是匹配整个字符串,你前后加个 .* 就可以了。
String[] as = { "Driver\\A ", "whatever1 ", "aaDriver\\B ", "abDriver\\Ccc ", "whatever2 " };
for(String str : as) {
if(str.matches( ".*Driver\\\\[A-Z].* ")) {
System.out.println(str);
}
}