midnite
midnite

Reputation: 5296

Return the "extended" length of a String

For Unicode characters, such as this No-Break Space ] [ of ASCII code 160, generated by holding the Alt key and typing 160 in the numpad, it yields a length of two characters.

For example, I have a field (e.g. the Tag field in Android log) of 88 characters. If I have 87 normal characters (ASCII < 127) and one No-Break Space, in String.length() I will still get 88. But in fact it needs a length of 89. And it exceeds the field size.

Is there a function in Java (or Android), that simply returns the "extended" length of a string?

FYI: http://en.wikipedia.org/wiki/Whitespace_character

Upvotes: 1

Views: 187

Answers (1)

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16152

Your mental model of Java characters is confused. That string has 88 characters, which when written out as a UTF-8 sequence need 89 bytes. Which version you will need to use depends on what you want to do with it, obviously. If you're going to use a network link to send data, you will need to use octets (bytes).

Upvotes: 1

Related Questions