目录
- 需求:将给定字符串提取子串
- 思路
- 代码
- 测试结果
- 结论
需求:将给定字符串提取子串
有个需求,需要将给定字符串提取子串,比如(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 = paandroidttern.matcher(v.get(i)); if(matcher.find()) { v.set(i, matcher.group(1)); } } http://www.devze.com 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 uKIGGFVtxLse 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(); } }
测试结果
如下:
结论
使用substringandroid+indexOf的方式速度最快。
以上就是java 获取子串速率比较分析的详细内容,更多关于java 获取子串速率的资料请关注编程客栈(www.devze.com)其它相关文章!
精彩评论