Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible problems with String reversing using charAt method

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.

like image 603
user1474111 Avatar asked Mar 14 '16 13:03

user1474111


People also ask

Does charAt work with strings?

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.

Can charAt return a string?

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.

Can charAt return multiple characters?

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.

What is the opposite of charAt () in Java?

toLowerCase(). charAt(y) == "y" , however, what you're looking for is remove. toLowerCase().


1 Answers

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];
}
like image 154
Maroun Avatar answered Oct 18 '22 18:10

Maroun