Java—初学—String类其他功能

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));
	  
}
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值