Effective method for creating string from char [], start, length

We are using java Sax to parse very large XML files Our roles are as follows:

@Override
public void characters(char ch[],int start,int length) throws SAXException {
    String value = String.copyValueOf(ch,start,length);
    ...
}

(the ch [] array passed by Sax is often very long)

However, we recently encountered some performance problems, and the analyzer showed us that more than 20% of the CPU utilization is higher than that of string Call to copyvalueof (it calls a new string (CH, length))

Is there a more effective way to get string from character array, starting index and length than string Copyvalueof (CH, length) or new string (CH, length)?

Solution

Good question, but I'm sure the answer is yes

This is because any string object construction uses the array copy method It cannot be constructed directly on an existing array because the string object must be immutable and its internal string array representation is encapsulated by external changes

In addition, in your case, you process fragments of an array It is impossible to build a string object on a fragment of another array in any way

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>