Reading one character at a time from a large string

13,858

I do not think there is a faster way. But please correct me!

A String instance is backed by a char array. charAt() does some index checks which may be the cause for it being slower than working with the array returned by toCharArray(). toCharArray() simply does a System.arraycopy() of the backing array.

Share:
13,858
ravi
Author by

ravi

Updated on August 09, 2022

Comments

  • ravi
    ravi over 1 year

    I have a big string having at most 100000 character. Instead of using string.charAt[index] to read a character from the string, I converted that string into char array using string.toCharArray() method and now i am working with charArray[index]. which takes less time than string.charAt[index] method. However i want to know that, is there any other way which is faster than string.toCharArray(); method?