String当中与转换相关的常用方法有:
1.public char[] toCharArray():将当前字符串拆分成为字符数组作为返回值。
2.public byte[] getBytes();获取当前字符串底层的字节数组。
3.public String replace(CharSequence oldString, CharSequence newString); eclipse 是Char oldString, char newString)
将所有出现的老字符串替换成为新的字符串,返回之后的结果为新的字符串
CharSequance意思是说可以接受字符串类型
public class Test06StringConvert {
public static void main(String[] args) {
//toCharArray() 转换成为字符数组
char[] charArray = "hello".toCharArray();
System.out.println(charArray[0]);//h
System.out.println(charArray.length);//5
//getBytes() 转换成为字节数组 就是最底层的Byte a97 b=98
byte[] bytes = "abc".getBytes();
System.out.println(bytes[0]);//97 下面遍历一遍
for(int e:bytes) {
System.out.print ("\t"+e);//97 98 99 =a b c
//String replace 游戏中的和谐字符
System.out.println("\n");
}
String str1="鲁班,草,你别送了 ?";
String str2=str1.replace("草", "***");
System.out.println(str2);// 鲁班,***,你别送了好吗?
}
}