Magik6k
Magik6k

Reputation: 125

Java: get length of string when repressented as bytes of some encoding

What is the best / cleanest way of determining size of Java String after conversion into byte array, preferably without first converting it into the array?

-EDIT By size I mean length of byte array that would be result of getBytes method

Upvotes: 2

Views: 424

Answers (1)

David Rabinowitz
David Rabinowitz

Reputation: 30448

There are encodings where you can know the length in bytes - all those which use fixed byte length per character. Examples are US-ASCII, ISO-8859-1 and UTF-16.

When using variable byte length, like the popular UTF-8 you cannot know a priory the length of the byte array.

Upvotes: 1

Related Questions