Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How String.charAt(int i) is implemented in Java?

If I want to check every char in a String using String.charAt(int i), would it count from start every time or it is converted to an array automatically and get the charAt index directly?

Would it be more efficient if I create a char array by String.toCharArray() and then go through the array by index?

Can I check this up in JavaDoc? Where?

like image 843
Zoe Avatar asked Jan 02 '14 23:01

Zoe


People also ask

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

Does charAt work on INT?

The Java String charAt(int index) method returns the character at the specified index in a string. The index value that we pass in this method should be between 0 and (length of string-1). For example: s. charAt(0) would return the first character of the string represented by instance s.

Does charAt work for numbers in Java?

To answer same we can define the charAt() function in java as the method by which we can return the character value for a particular index number belongs to a string while executing the charAt() method. Simply, it is the method which returns the character that is present in the string specified by its index.


1 Answers

The JRE is mostly open source. You can download a zip file here or browse online with websites like grepcode.

String.charAt:

public char charAt(int index) {
    if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index];
}
like image 184
Jeffrey Avatar answered Oct 01 '22 21:10

Jeffrey