字符串的转换相关方法

字符串的转换相关方法

String当中与转换相关的常用方法:

public char[] toCharArray();将当前字符串拆分成为字符数组作为返回值.

public byte[] getBytes();获得当前字符串底层的字节数组

public String replace(CharSequence oldString,CharSequence newString);

将所有出现的老字符串替换成为新的字符串,返回替换之后的结果为新字符串.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

public class demo11{
public static void main(String[] args) {
String str1 = "我爱你";

char[] chArray = str1.toCharArray();//将str1转换为char[]数组
byte[] by = str1.getBytes();//将str1转换为byte[]数组;

System.out.println(chArray[0]);

for (int i = 0; i < by.length; i++) {
System.out.println(by[i]);
}

System.out.println("========================");
String str2 = "你麻痹的,你会不会啊";
String str3 = str2.replace("麻痹", "**");//str2.replace(要替换的字符串,替换的字符);
System.out.println(str3);


}
}