I saw a comment here that all solutions with charAt
are wrong. I could not exactly understand and find something about charAt
on internet. As I look the source code it just returns an element from the char array. So my question is that if there any problem or issue about using charAt
?
Comment is like that
Strictly speaking, all the solutions based on charAt are wrong, as charAt doesn't give you the "character at", but the "code unit at", and there are code units that are not characters and characters that need multiple code units.
The 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.
Return valueA string representing the character (exactly one UTF-16 code unit) at the specified index . If index is out of range, charAt() returns an empty string.
The Java charAt() method returns a character at a specific index position in a string. The first character in a string has the index position 0. charAt() returns a single character. It does not return a range of characters.
toLowerCase(). charAt(y) == "y" , however, what you're looking for is remove. toLowerCase().
Different characters are encoded with a different numbers of bytes (using UTF-16 scheme). For example, the "A" character is represented as follows:
01000001
So far so good.
But if you have a character like 𝔴, you'll have a problem. Its UTF-16 representation (BE) is:
11011000 00110101 11011101 00110100
And then charAt
can indeed return the second code unit for that character.
See the JDK 7 implementation of String#charAt
:
public char charAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index + offset];
}
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