简体   繁体   English

不同语言的 String.length() 计数不同

[英]Different count for String.length() for different languages

What I want我想要的是

Given a String s and a coordinate x1, want to print to pdf considering x1 at the center.给定一个字符串 s 和一个坐标 x1,想打印到 pdf 考虑 x1 在中心。 I need starting x-coordinate我需要开始 x 坐标

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)我可以很容易地用 x1-s.length()/2 计算起始坐标(x)到它在中心的位置,但它对于印地语(语言)失败了

How to count the actual no of characters ignoring the Unicode characters but the letters.如何计算忽略Unicode字符但忽略字母的实际字符数。 I basically want to print the string at the center, String is variable我基本上想在中心打印字符串,字符串是可变的

There are a couple of reasons why the length method might get you an unexpected answer. length方法可能会为您提供意想不到的答案有几个原因。

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. String上的 length 方法返回String中 char 值的数量,但 Unicode 字符比可能的char值多,因此对于某些字符,使用多个 char 值来存储字符。 To get the length you expect for, try this: s.codePointCount(0, s.length())要获得您期望的长度,请尝试以下操作: s.codePointCount(0, s.length())

If your string contains a Combining Diacritical Mark , that uses one char, but is displayed combined with another character.如果您的字符串包含Combining Diacritical Mark ,它使用一个字符,但与另一个字符组合显示。 Here's a question about how to spot those: detect any combining character in Java这是一个关于如何发现这些问题的问题: 检测 Java 中的任何组合字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM