zoukankan      html  css  js  c++  java
  • Java字符串中常用字符占用字节数

    java中一个char型的数据(也就是一个字符)占两个字节。而Java中常用的字符包括数字、英文字母、英文符号、中文汉字、中文符号等,若在字符串中包含里面的多种字符,它们是否都占两个字符呢?答案是否定的。

    public class CharBytes {
        public static void main(String[] args) {
            String s1 = "1234567";// 7个数字字符
            byte[] b1 = s1.getBytes();
            System.out.println("7个数字字符1234567所占的字节数为:" + b1.length);
            String s2 = "abcdefg";// 7个英文字符
            byte[] b2 = s2.getBytes();
            System.out.println("7个英文字符abcdefg所占的字节数为:" + b2.length);
            String s3 = "::<>{}?";// 7个英文符号字符
            byte[] b3 = s3.getBytes();
            System.out.println("7个英文符号字符::<>{}?所占的字节数为:" + b3.length);
            String s4 = "钓鱼岛是中国的";// 7个中文汉字字符
            byte[] b4 = s4.getBytes();
            System.out.println("钓鱼岛是中国的所占的字节数为:" + b4.length);
            String s5 = "【】《》?:!";// 7个中文符号字符
            byte[] b5 = s5.getBytes();
            System.out.println("7个中文符号字符  【】《》?:!  所占的字节数为:" + b5.length);
            String s6 = "/n";
            byte[] b6 = s6.getBytes();
            System.out.println("/n所占的字节数为:" + b6.length);
        }
    }

    运行结果为:

    7个数字字符1234567所占的字节数为:7
    7个英文字符abcdefg所占的字节数为:7
    7个英文符号字符::<>{}?所占的字节数为:7
    钓鱼岛是中国的所占的字节数为:14
    7个中文符号字符  【】《》?:!  所占的字节数为:14
    /n所占的字节数为:2
  • 相关阅读:
    ORA-12170 TNS 连接超时
    判断字符串里面是否包含汉字
    关于Try/Catch 代码块
    SOLID 原则
    整洁代码原则
    装箱 拆箱
    存储过程优缺点
    ANTLR Tool version 4.4中使用ANTLR 4.7.1
    The Apache Tomcat installation at this directory is version 8.5.40. A Tomcat 8.0 installation is expected.
    实现功能:构建一个JSON到XML的翻译器。
  • 原文地址:https://www.cnblogs.com/zhangwuji/p/9719518.html
Copyright © 2011-2022 走看看