package cn.itcast.day12_homework;
public class StringDemo {
/**
* 替换功能
* String replace(char oldChar,char newChar):用新的字符去替换指定的旧字符
* String replace(String oldString,String newString):用新的字符串去替换指定的旧字符串
*
* 切割功能
* String[] split(String regex)
*
* 去除字符串两端空格
* String trim()
*
* 按字典顺序比较两个字符串
* int compareTo(String str)
*/
public static void main(String[] args) {
// 替换功能
// String replace(char oldChar,char newChar):用新的字符去替换指定的旧字符
String str = " helloworld12345 ";
System.out.println(str.replace('r', '4'));
System.out.println(str.replace("ll", "star"));
// 切割功能
// String[] split(String regex)
for (int i = 0; i < str.split("o").length; i++) {
System.out.println(str.split("o")[i]);
}
for (int i = 0; i < str.split("l").length; i++) {
System.out.println(str.split("l")[i]);
}
// 去除字符串两端空格
// String trim()
System.out.println(str.trim());
// 按字典顺序比较两个字符串
// int compareTo(String str)
System.out.println(str.compareTo(" hello"));
String s1 = "hello";
String s2 = "aello";
String s3 = "mello";
String s4 = "hello";
String s5 = "Hello";
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s1.compareTo(s4));
System.out.println(s1.compareTo(s5));
}
}
|
|