Mike
Mike

Reputation: 11

Fontmetrics stringwidth vs string length

I have an issue where I need to test the character at the stringwidth of 256. However, when I do a charAt(index) test, I get an indexoutofbounds exception because 256 (buffered image width) is actually longer than the string.length. My question is once I hit the stringwidth of 256, how do i test the character at that width?

Upvotes: 1

Views: 336

Answers (2)

mattiash
mattiash

Reputation: 172

You would need to use the charWidth() method for all characters in your string to know which character occurs at width 256.

"I need to wrap the string in increments of 256 at the space char" - what does it mean? You could always render it to an offscreen buffer and use drawImage() to render it in 256 pixels wide chunks.

Upvotes: 0

Bohemian
Bohemian

Reputation: 425428

Java, as with all "C like" languages, uses zero-based indexing, ie the first element of an array (or collection etc) has index 0, the second has index 1, etc

To access the 256th char, use charAt(255).

Upvotes: 1

Related Questions