替换功能
String replace(char old,char new)
String replace(String old,String new)
去除字符串两空格
String trim()
按字典顺序比较两个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)
package com.shujia.java.day15;
/*
替换功能
String replace(char old,char new)
String replace(String old,String new)
去除字符串两空格 ******
String trim()
按字典顺序比较两个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)
*/
public class StringDemo11 {
public static void main(String[] args) {
String s = "helloworld";
//String replace(char old,char new)
//将字符串中所有的l替换成a,返回一个新的字符串
String replaceString = s.replace('l', 'a');
System.out.println(replaceString);
System.out.println(s);
//String replace(String old,String new)
String replaceString2 = s.replace("owo", "wow");
System.out.println(replaceString2);
String replaceString3 = s.replace("owo", "qwerdf");
System.out.println(replaceString3);
//如果被替换的字符串不存在,返回原来的字符串
String replaceString4 = s.replace("opi", "qwe");
System.out.println(replaceString4);
//String trim()去除字符串两边的空格,不能去除字符串中间的空格
String s1 = " hello world ";
String trimString = s1.trim();
System.out.println(s1);
System.out.println(trimString);
//int compareTo(String str)
String s2 = "hello"; //h的ASCLL码值:104
String s3 = "hello";
String s4 = "abc"; // a的ASCLL码值:97
String s5 = "qwe"; // q的ASCLL码值:113
System.out.println(s2.compareTo(s3)); // 0
System.out.println(s2.compareTo(s4)); // 7
System.out.println(s2.compareTo(s5)); // -9
//如果前面的几个字符都是一样的怎么办呢?
String d = "hello";
String d3 = "hellobigdata";
String d2 = "hel";
System.out.println(d.compareTo(d2));//d2-d 2
System.out.println(d3.compareTo(d));//d3-d 7
}
}