有个需求,需要将给定字符串提取子串,比如(aaa.xx)变成aaa,有几个思路。

  1. 使用正则模式匹配,然后替换
  2. 使用正则替换
  3. 使用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();
    }
}

测试结果如下:
image.png

结论:
使用substring+indexOf的方式速度最快。


点墨
26 声望3 粉丝

全栈前端开发工程师