A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 大牛1 中级黑马   /  2016-5-31 10:53  /  478 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

String类的转换功能
  1. package cn.itcast_05;

  2. /*
  3. * String的转换功能:
  4. * byte[] getBytes():把字符串转换为字节数组。
  5. * char[] toCharArray():把字符串转换为字符数组。
  6. * static String valueOf(char[] chs):把字符数组转成字符串。
  7. * static String valueOf(int i):把int类型的数据转成字符串。
  8. *                 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
  9. * String toLowerCase():把字符串转成小写。
  10. * String toUpperCase():把字符串转成大写。
  11. * String concat(String str):把字符串拼接。
  12. */
  13. public class StringDemo {
  14.         public static void main(String[] args) {
  15.                 // 定义一个字符串对象
  16.                 String s = "JavaSE";

  17.                 // byte[] getBytes():把字符串转换为字节数组。
  18.                 byte[] bys = s.getBytes();
  19.                 for (int x = 0; x < bys.length; x++) {
  20.                         System.out.println(bys[x]);
  21.                 }
  22.                 System.out.println("----------------");

  23.                 // char[] toCharArray():把字符串转换为字符数组。
  24.                 char[] chs = s.toCharArray();
  25.                 for (int x = 0; x < chs.length; x++) {
  26.                         System.out.println(chs[x]);
  27.                 }
  28.                 System.out.println("----------------");

  29.                 // static String valueOf(char[] chs):把字符数组转成字符串。
  30.                 String ss = String.valueOf(chs);
  31.                 System.out.println(ss);
  32.                 System.out.println("----------------");

  33.                 // static String valueOf(int i):把int类型的数据转成字符串。
  34.                 int i = 100;
  35.                 String sss = String.valueOf(i);
  36.                 System.out.println(sss);
  37.                 System.out.println("----------------");

  38.                 // String toLowerCase():把字符串转成小写。
  39.                 System.out.println("toLowerCase:" + s.toLowerCase());
  40.                 System.out.println("s:" + s);
  41.                 // System.out.println("----------------");
  42.                 // String toUpperCase():把字符串转成大写。
  43.                 System.out.println("toUpperCase:" + s.toUpperCase());
  44.                 System.out.println("----------------");

  45.                 // String concat(String str):把字符串拼接。
  46.                 String s1 = "hello";
  47.                 String s2 = "world";
  48.                 String s3 = s1 + s2;
  49.                 String s4 = s1.concat(s2);
  50.                 System.out.println("s3:"+s3);
  51.                 System.out.println("s4:"+s4);
  52.         }
  53. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马