public class Test { public void request(String b,int rows){ String a = "cd1212121212jjk,cd12121212cd12jieie,cd123"; String[] c = a.split(b); for (int i = 0; i < c.length; i++) { if(c[i].length()>0){ System.out.println((b+c[i]).substring(0,rows)); } } } public static void main(String[] args) { Test test = new Test(); test.request("cd", 3); } }
没有注意还有个int indexOf(String str,int fromIndex)
lastIndexOf也有
String a = "cd1212121212jjk,cd12121212cd12jieie,cd123";
String[] str = a.split(",");
for(int i=0; i<str.length; i++){
if(str[i].startsWith("cd") && str[i].length() == 5){
System.out.println(str[i]);
}
}
楼主是这个意思不?
楼主是想这样么?
String s = "cd1212121212jjk,cd12121212cd12jieie";
Matcher matcher = Pattern.compile("(cd.)").matcher(s);
while( matcher.find() ) {
System.out.println(matcher.group());
}
public class Test {
public void request(String b,int rows){
String a = "cd1212121212jjk,cd12121212cd12jieie,cd123";
String[] c = a.split(b);
for (int i = 0; i < c.length; i++) {
if(c[i].length()>0){
System.out.println((b+c[i]).substring(0,rows));
}
}
}
public static void main(String[] args) {
Test test = new Test();
test.request("cd", 3);
}
}