-
字符串截取方法:
- public String substring(int index):从index位置截取到末尾
- public String substring(int begin,int end):截取【begin,end)
public static void main(String[] args){
String s1="RitaLove";
String s2="Code";
String s3=s1.substring(4);//Love
System.out.println(s3);//Love
String s="Hello";
System.out.println(s);//Hello
s="World";
System.out.println(s);//World
//为什么两次输出的s不一样?字符串的内容是没有改变的,依旧存在“Hello”“World”
//这两个字符串,s中保存的是地址值,开始s指向Hello,之后s地址值变成了World的地址值,指向World
}
-
字符串的转换相关方法:
- public char[] toCharArray():将当前字符串转换为字符数组
- public byte[] getBytes():获得当前字符串的底层字节数组
- public String replace(CharSequence oldString,CharSequence newString):字符串所有oldString的地方替换为newString
public static void main(String[] args){
char[] chars="Rita".toCharArray();
System.out.println(chars[0]);//R
System.out.println(chars.lengrh);//4
String s="Fuck you!";
String s1=s.replace("Fuck","****");//屏蔽敏感词汇
System.out.println(s);//**** you!
}