有个需求,需要将给定字符串提取子串,比如(aaa.xx)变成aaa,有几个思路。
- 使用正则模式匹配,然后替换
- 使用正则替换
- 使用substring
代码如下:
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class main {
public static int len = 10000000;
public static void useRegMatch(){
LocalDateTime start = LocalDateTime.now();
List<String> v = new ArrayList<String>();
for(int i = 0; i < len; ++i) {
v.add("230202"+i+i+".IB"+i);
}
//handle use reg
Pattern pattern = Pattern.compile("^(.+)(\\.)(.+)$");
Matcher matcher;
for(int i = 0; i < v.size();++i){
matcher = pattern.matcher(v.get(i));
if(matcher.find()) {
v.set(i, matcher.group(1));
}
}
LocalDateTime end = LocalDateTime.now();
System.out.println("useRegMatch,waste:"+Duration.between(start, end).toMillis()+" "+v.get(0)+" "+v.get(1));
}
public static void useRegReplace(){
LocalDateTime start = LocalDateTime.now();
List<String> v = new ArrayList<String>();
for(int i = 0; i < len; ++i) {
v.add("230202"+i+i+".IB"+i);
}
//handle use reg
for(int i = 0; i < v.size();++i){
String newV = v.get(i).replaceAll("(\\.)(.+)$","");
v.set(i, newV);
}
LocalDateTime end = LocalDateTime.now();
System.out.println("useRegReplace,waste:"+Duration.between(start, end).toMillis()+" "+v.get(0)+" "+v.get(1));
}
public static void useSpliceIndexOf(){
LocalDateTime start = LocalDateTime.now();
List<String> v = new ArrayList<String>();
for(int i = 0; i < len; ++i) {
v.add("230202"+i+i+".IB"+i);
}
//handle use reg
for(int i = 0; i < v.size();++i){
v.set(i, v.get(i).substring(0,v.get(i).indexOf(".")));
}
LocalDateTime end = LocalDateTime.now();
System.out.println("useRegReplace,waste:"+Duration.between(start, end).toMillis()+" "+v.get(0)+" "+v.get(1));
}
public static void main(String[] args) {
useRegMatch();
useRegReplace();
useSpliceIndexOf();
}
}
测试结果如下:
结论:
使用substring+indexOf的方式速度最快。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。