替换功能
- String replace(char old, char new);
String replace(String old, String new);
/*** String类的字符串替换*/private static void demo1() {//演示String类的替换功能String s = "hello world";String s1 = s.replace("world","axin");System.out.println(s);//hello worldSystem.out.println(s1);//hello axinString replace = s.replace("hhh", "www");System.out.println(replace); //hhh不存在的情况下,保持不变}
去除字符串及两边空格
String trim();
/*** 去除字符串两端的空格*/private static void demo2() {String s = " hello world ";String trim = s.trim();System.out.println(s);//" hello world "System.out.println(trim);//hello world //只替换两端的空格}
按字典排序比较两个字符串
int compareTo(String str)
- int compareToIgnoreCase(String str)
public static void main(String[] args) {String s1 = "bcdef";String s2 = "a";/*s1 < s2 结果为小于0的数s1 = s2 返果为0s1 > s2 结果为大于0的数*/int i = s1.compareTo(s2);//按照码表值比较 unicode码表值System.out.println(i);String s3 = "中";String s4 = "国";int i1 = s3.compareTo(s4);System.out.println('中' + 0);//20013System.out.println('国' + 0);//22269System.out.println(i1);//-2256String s5 = "ABC";String s6 = "abc";System.out.println(s5.compareTo(s6));//-32System.out.println(s5.compareToIgnoreCase(s6));//0/*compareToIgnoreCase()的源码:public int compare(String s1, String s2) {int n1 = s1.length();int n2 = s2.length();int min = Math.min(n1, n2);for (int i = 0; i < min; i++) {char c1 = s1.charAt(i);char c2 = s2.charAt(i);if (c1 != c2) {c1 = Character.toUpperCase(c1); //防止一个大写 一个小写,统一转换成大写c2 = Character.toUpperCase(c2);if (c1 != c2) {c1 = Character.toLowerCase(c1); //统一转换成小写c2 = Character.toLowerCase(c2);if (c1 != c2) {// No overflow because of numeric promotionreturn c1 - c2;}}}}return n1 - n2;}*/}
