用集合java字符串第一个单词,在Java中删除字符串中第一个单词的最佳方法

What is the fastest way of getting rid off the first token in a string? So far, I've tried this:

String parentStringValue = this.stringValue.split(" ", 2)[1];

and it's extremely memory and speed inefficient (when repeated millions of times for 15 word long strings). Suppose the string is made of tokens separated by spaces.

解决方案

StringBuilder vs substring( x ) vs split( x ) vs Regex

Answer Edited : Major Flaws Corrected

After correcting for some fairly major flaws in my benchmarking (as pointed out by Jay Askren in the comments). The StringBuilder method came out as the fastest by a significant margin (although this assumes that the StringBuilder objects were pre-created), with substring coming out in second place. split() came out second to last at 10x slower than the StringBuilder method.

ArrayList strings = new ArrayList();

ArrayList stringBuilders = new ArrayList();

for(int i = 0; i < 1000; i++) strings.add("Remove the word remove from String "+i);

for(int i = 0; i < 1000; i++) stringBuilders.add(new StringBuilder(i+" Remove the word remove from String "+i));

Pattern pattern = Pattern.compile("\\w+\\s");

// StringBuilder method

before = System.currentTimeMillis();

for(int i = 0; i < 5000; i++){

for(StringBuilder s : stringBuilders){

s.delete(0, s.indexOf(" ") + 1);

}

}

after = System.currentTimeMillis() - before;

System.out.println("StringBuilder Method Took "+after);

// Substring method

before = System.currentTimeMillis();

for(int i = 0; i < 5000; i++){

for(String s : strings){

String newvalue = s.substring(s.indexOf(" ") + 1);

}

}

after = System.currentTimeMillis() - before;

System.out.println("Substring Method Took "+after);

//Split method

before = System.currentTimeMillis();

for(int i = 0; i < 5000; i++){

for(String s : strings){

String newvalue = s.split(" ", 2)[1];

System.out.print("");

}

}

after = System.currentTimeMillis() - before;

System.out.println("Your Method Took "+after);

// Regex method

before = System.currentTimeMillis();

for(int i = 0; i < 5000; i++){

for(String s : strings){

String newvalue = pattern.matcher(s).replaceFirst("");

}

}

after = System.currentTimeMillis() - before;

System.out.println("Regex Method Took "+after);

I ran the above in random orders, after a warm up, in succession taking averages, increased the number of operations from 5 million to 30 million, and ran each ten times before moving on to the next. Either way the order of fastest to slowest stayed the same. Below is some sample output from the code above;

StringBuilder Method Took 203

Substring Method Took 588

Split Method Took 1833

Regex Method Took 2517

It is worth mentioning that calling split() with a String with a length greater than 1 simply uses Regex in its implementation and so there should be no difference between using split() and a Pattern object.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值