1 /* 2 String当中与转换相关常用的方法有: 3 4 public char[] toCharArray():将当前字符串拆分成字符数组作为返回值。 5 public byte[] getBytes():获取当前字符串底层的字节数组。 6 public String replace(CharSequence oldString, CharSequence newString): 7 将所有出现的老字符串替换成为新的字符串,返回值替换之后的结果成新字符串。 8 备注:CharSequence意思就是说可以接受字符串类型。 9 */ 10 public class Demo02 { 11 public static void main(String[] args){ 12 //转换成为字符数组 13 char[] chars = "Hello".toCharArray(); 14 System.out.println(chars[0]);//H 15 System.out.println(chars.length);//5 16 System.out.println("==============="); 17 //转换成为字节数组 18 byte[] bytes = "abc".getBytes(); 19 for (int i = 0; i < bytes.length; i++){ 20 System.out.println(bytes[i]); 21 } 22 System.out.println("==============="); 23 24 String str1 = "How do you do ?"; 25 String str2 = str1.replace("o","*"); 26 System.out.println("替换成功!"+str2); 27 System.out.println("==============="); 28 29 String lang1 = "会不会玩啊!你大爷的! 你大爷的! 你大爷的!"; 30 String lang2 = lang1.replace("你大爷的","*"); 31 System.out.println(lang2); 32 33 } 34 }