有哪些方法可以避免 String.substring 返回带有无效 unicode 字符的子字符串? char obsoleteCode point

2022-09-02 13:16:59

最近,只有我注意到,有可能返回带有无效unicode字符的字符串。substring

例如

public class Main {

    public static void main(String[] args) {
        String text = "						

答案 1

char obsolete

The char type has been legacy since Java 2, essentially broken. As a 16-bit value, char is physically incapable of representing most characters.

Your discovery suggests that the String#substring command is char based. Hence the problem shown in your code.

Code point

Instead, use code point integer numbers when working with individual characters.

int[] codePoints = "						

答案 2

推荐