StringDemo

package cn.zuoye;
/*
 * string类的其他功能
 *
 * 替换功能
 * string replace (char old ,char new)
 * string replace (string old,string new)
 *
 * 去除字符串两个空格(两边)
 * string trim()
 *
 * 按字典顺序比较两个字符串
 * int compareTo(String str)
 * int compareToIgnoreCase(String str)
 */
public class StringDemo {
 public static void main(String[] args) {
  // 替换功能
  String s1 = "helloworld";
  String s2 = s1.replace('l', 'k');
  System.out.println("s1:"+s1);//s1:helloworld
  System.out.println("s2:"+s2);//s2:hekkoworkd
  
  //去除字符串两空格
  String s3 = "  hello world   ";
  String s4 = s3.trim();
  System.out.println("s3:"+s3+"++++");//s3:  hello world   ++++
  System.out.println("s4:"+s4+"++++");//s4:hello world++++
  
  //按字典顺序比较两个字符串
  String s5 = "hello";
  String s6 = "hello";
  String s7 = "abc";
  String s8 = "xyz";
  System.out.println(s5.compareTo(s6));//0
  System.out.println(s5.compareTo(s7));//7
  System.out.println(s5.compareTo(s8));//-16
    
 }
}
posted @ 2018-11-02 09:30  阿蓉  阅读(242)  评论(0编辑  收藏  举报