1

What I want

Given a String s and a coordinate x1, want to print to pdf considering x1 at the center. I need starting x-coordinate

problem

I can easily calculate start coordinate(x) with x1-s.length()/2 to a position it at the center but it fails for Hindi(Language)

How to count the actual no of characters ignoring the Unicode characters but the letters. I basically want to print the string at the center, String is variable

Islam Hassan
  • 673
  • 1
  • 10
  • 21

1 Answers1

0

There are a couple of reasons why the length method might get you an unexpected answer.

The length method on String returns the number of char values in the string, but there are more Unicode characters than possible char values, so for some characters more than one char value is used to store the character. To get the length you expect for, try this: s.codePointCount(0, s.length())

If your string contains a Combining Diacritical Mark, that uses one char, but is displayed combined with another character. Here's a question about how to spot those: detect any combining character in Java

J Banana
  • 154
  • 6