zoukankan      html  css  js  c++  java
  • 字符串与字符数组的多种转换方式。

    public class StringAPIDemo1 {
    public static void main(String[] args) {
    String str = "HELLO";
    char c[] = str.toCharArray(); // 将字符串变为字符数组

    for (int i = 0; i < c.length; i++) {
    System.out.print(c[i] + " ");
    }
    System.out.println(); //换行
    String str2 = new String(c);// 将整个字符数组转换为字符串
    String str3 = new String(c, 0, 3);// 将部分字符数组转换为字符串
    System.out.println(str2);
    System.out.println(str3);
    }

    private static void test2() {
    String str1 = "霍元甲";
    byte b[] = str1.getBytes();// 将字符串转为byte数组
    System.out.println(new String(b)); // 将整个byte数组转换为字符串
    System.out.println(new String(b, 0, 3)); // 将部分byte数组转换为字符串
    for (int i = 0; i < b.length; i++) {
    System.out.println(b[i]);
    }

    }


    }


    }

  • 相关阅读:
    MVC 4 中 WEB API 选择 返回格式
    用XML配置菜单的一种思路,附一些不太准确的测试代码
    2020.11.15(每周总结)
    2020.11.19
    2020.11.22
    2020.11.21
    2020.11.14
    202.11.13
    2020.11.20
    2020.11.17
  • 原文地址:https://www.cnblogs.com/wangffeng293/p/13293293.html
Copyright © 2011-2022 走看看