简体   繁体   English

不使用String.split()拆分StringBuilder值

[英]splitting a StringBuilder value without String.split()

I'll be processing texts and will split along the way for one, its substrings on regular expressions like String.split() does. 我将处理文本,并一路拆分,它的子字符串位于正则表达式上,例如String.split() i'm looking to do these on StringBuilder for speed concerns. 我希望在StringBuilder上进行这些操作以解决速度问题。

From what i know, StringBuilder has no such method. 据我所知,StringBuilder没有这种方法。 all i can do is to invoke String.split() and turn the result into StringBuilder []. 我所能做的就是调用String.split()并将结果转换为StringBuilder []。 however, this would be slow-- String.split() interns every entry of String[] it produces. 但是,这会很慢-String.split ()会实习生成的String []的每个条目。

Also - StringBuilder isn't overriding Object.equals() . 另外-StringBuilder不会覆盖Object.equals() when i need to use StringBuilder as the type in collections, i'm writing a wrapper class that has a StringBuilder value-field and overriding equals() on the values of this field to get it right. 当我需要使用StringBuilder作为集合中的类型时,我正在编写一个具有StringBuilder值字段的包装器类,并对该字段的值覆盖equals()使其正确。

these are the 2 so far i can recall that i needed to have and that came short. 这些是到目前为止的2个,我还记得我需要拥有,而且还很短。

My Q is: 我的问是:

Am i missing something here - is there a way to get these functionalities on StringBuilder without having String in between to slow it down? 我是否在这里缺少任何东西-有没有办法在StringBuilder上获得这些功能而又没有中间的String来减慢它的速度?

If not - why not? 如果没有-为什么不呢? The main reason-for-being of StringBuilder is the execution time at the cost of memory-- as alternative to String's efficient memory with slow execution(?) And StringBuilder is a type for texts. 之所以成为StringBuilder的主要原因,是因为执行时间是以内存为代价的-替代了String的高效内存(执行缓慢)(?),而StringBuilder是文本的一种类型。 why wouldn't it have these directly-- split() for one? 为什么不直接使用这些-split()呢?

TIA. TIA。

StringBuilder is a CharSequence too. StringBuilder也是一个CharSequence。

Pattern pattern = Pattern.compile(",\\s*"); // Best static final.
Matcher m = pattern.matcher(stringBuilder);
int pos0 = 0;
while (m.find()) {
    int pos1 = m.start();
    CharSequence cs = stringBuilder.subSequence(pos0, pos1);
    ...
    pos0 = pos1;
}
CharSequence cs = stringBuilder.subSequence(pos0, stringBuilder.length());
...

Of course s being either CharSequence or String. 当然s是要么为CharSequence或字符串。 A String could utilize String.substring which does not allocate a new char array. 字符串可以利用不分配新字符数组的String.substring

A regular expression still is slow. 正则表达式仍然很慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM