I want to find the character at a particular position of a very large string. However i am unable to use charAt()
method because the range exceeds that of int. Is there a tweak to this?
Definition and UsageThe charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.
It takes the character which is at index i in str and substracts the ASCII value of the character 'a'.
charAt(i) will return a character and string. charAt(i)- '0' will return the actual integer value. For e.g. string="12345",this method will return an integer array [1,2,3,4,5].
In Java, Strings are backed by a character array. The theoretical size of an array is limited by the maximum value of int
, so it's impossible to have a String with over 231-1 characters to begin with.
To overcome this issue you can create a string class of your own that uses multiple arrays or strings as storage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With