Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String's CharAt method using long

Tags:

java

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?

like image 748
user2133404 Avatar asked Jan 24 '14 21:01

user2133404


People also ask

Can you use charAt for string?

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.

What does STR charAt I -' a do?

It takes the character which is at index i in str and substracts the ASCII value of the character 'a'.

What is the meaning of charAt i )-' 0?

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].


1 Answers

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.

like image 105
Joni Avatar answered Oct 02 '22 16:10

Joni