在Java中删除字符串中第一个单词的最佳方法 [英] best way to remove the first word in a string in Java

查看:237
本文介绍了在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];

并且它的内存和速度极低(对于15个字长的字符串重复数百万次)。假设字符串由用空格分隔的标记组成。

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



答案已编辑:已修正主要缺陷

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

Answer Edited : Major Flaws Corrected

在我的基准测试中纠正了一些相当重大的缺陷(正如Jay Askren在评论中指出的那样)。 StringBuilder 方法以最快的速度出现(虽然这假设 StringBuilder 对象是预先创建的),子串出现在第二位。 split()倒数第二,比<​​code> StringBuilder 方法慢10倍。

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<String> strings = new ArrayList<String>();
  ArrayList<StringBuilder> stringBuilders = new ArrayList<StringBuilder>();
  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);

我以随机顺序运行上述内容,经过热身后,连续取平均值,增加数量从500万到3000万的运营,每次运行十次,然后继续下一次。无论哪种方式,最快到最慢的顺序保持不变。下面是上面代码的一些示例输出;

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

值得一提的是,调用 split(),长度大于1的 String 只是在其实现中使用Regex,因此使用之间应该没有区别 split() Pattern 对象。

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.

这篇关于在Java中删除字符串中第一个单词的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆