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?
It takes the character which is at index i in str and substracts the ASCII value of the character 'a'.
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.
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.
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];
}
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