package com.gaowei_06;
/*
* 替换功能
* 1. String replace(char old,char new)
* 2. String replace(String old,String new)
*
* 去除两端空格
* 3. s.trim()
*
* 按字典比较两个字符串
* 4. s.compareTo()
*
*/
public class StringDemo12 {
public static void main(String[] args) {
String s = "please help yourself!";
System.out.println("-------1-----------");
String s1 = s.replace('r', 'w');
System.out.println(s1);
System.out.println("-------2-----------");
String s2 = s.replace("rse", "wrw");
System.out.println(s2);
System.out.println("-------3-----------");
String s3 = " please come on ";
System.out.println(s3.trim());
System.out.println("--------4----------");
String s4 = "hel";
String s5 = "hell";
String s6 = "hallo";
String s7 = "abc";
String s8 = "xyz";
System.out.println(s4.compareTo(s5)); //两个长度不相等,且短字符串是长字符串的子串,则返回的是两者的长度差
System.out.println(s4.compareTo(s6));
System.out.println(s4.compareTo(s7));
System.out.println(s4.compareTo(s8));
}
}
运行结果: