StringBuilder performance and substrings

About a performance issue related to operation Stringin Java:

String s = ".........................,--content I wanted---,....";

I will mainly use a loop forto iterate over a long string and extract the contents between ,.

Usage substring- write the index of the beginning and end of the index during the iteration, and then do it s.subtring(begin,end).

Using StringBuilder, I will be appendevery charbetween commas during iteration.

Is there a performance issue? I mean, which one will be faster when I have a lot of such operations about the extractingcontents of the string.

+1
source share
1

string.substring , StringBuilder.

Pre Java7u6, substring String, ( char) . , :

 String(int offset, int count, char value[]) {
     this.value = value;
     this.offset = offset;
     this.count = count;
 }

, :

 Arrays.copyOfRange(value, offset, offset+count);

, Arrays.copyOfRange System.arraycopy, , , , append.

+3

All Articles