由于前段时间才学习正则,第一时间就想到了正则:
public int strStr(String haystack, String needle) {
if(haystack==null||needle==null){
return -1;
}
Pattern p=Pattern.compile(needle);
Matcher m=p.matcher(haystack);
if(m.find()){
return m.start();
}
return -1;
}
于是点进去一看其实只需要一行:
return haystack.indexOf(needle);
只能说自己对字符串这块的java操作除了spirt以外真的是一无所知