å°±æ¯äºæ¥¼è¯´çé£æ ·ã
1. substring();
public class Temp {
public static void main(String[] args) {
String str = "1234567890";
//æ¯æ®µçé¿åº¦
int step = 2;
int length = str.length();
for (int i = 0; i < length; i += step){
//ç»æçä½ç½®
int end = i + step;
if (end > length){
//é¿å
è¶ç
end = length;
}
System.out.println(str.substring(i, end));
}
}
}
2. æ£å表达å¼
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test {
public static void main(String[] args){
String str = "1234567890";
Pattern regex = Pattern.compile(".."); //å¹é
两个å符
Matcher m = regex.matcher(str); //è·åå¹é
对象
while(m.find()){
System.out.println(m.group()); //è¾åºå¹é
çæ¯ä¸ç»å
容
}
}
}