转载

一个Java字符串中到底有多少个字符?

依照Java的文档, Java中的字符内部是以UTF-16编码方式表示的,最小值是/u0000(0),最大值是/uffff(65535), 也就是一个字符以2个字节来表示,难道Java最多只能表示 65535个字符?

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '/u0000' (or 0) and a maximum value of '/uffff' (or 65,535 inclusive).

from The Java™ Tutorials

首先,让我们先看个例子:

public class Main {    public static void main(String[] args) {        // 中文常见字        String s = "你好";        System.out.println("1. string length =" + s.length());        System.out.println("1. string bytes length =" + s.getBytes().length);        System.out.println("1. string char length =" + s.toCharArray().length);        System.out.println();        // emojis        s = "
原文  http://www.udpwork.com/item/17272.html
正文到此结束
Loading...