String的用法——其他功能

package cn.itcast_06;
/*

  • 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) {
	// TODO Auto-generated method stub
	//替换功能
	String s1 = "helloworld";
	String s2 = s1.replace('l', 'k');
	String s3 = s1.replace("ll", "kk");
	String s4 = s1.replace("ll", "kkkkk");
	
	System.out.println("s1:" + s1);//helloworld
	System.out.println("s2:" + s2);//hekkoworkd
	System.out.println("s3:" + s3);//hekkoworld
	System.out.println("s4:" + s4);//hekkkkkoworld
	
	//去除字符串两端空格:String trim()
	String s5 = "         hello     world                  ";
	String s6 = s5.trim();
	System.out.println("s5:" + s5 + "---");
	System.out.println("s6:" + s6 + "---");
	
	//按字典顺序比较两个字符串(大小)
	String s7 = "hello";
	String s8 = "hfllo";
	String s9 = "gbc";
	String s10 = "xyz";
	System.out.println(s7.compareTo(s7));//0
	System.out.println(s7.compareTo(s8));//-1
	System.out.println(s7.compareTo(s9));//1
	System.out.println(s7.compareTo(s10));//-16
	
}

}

posted @   行走在代码边缘  阅读(115)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
点击右上角即可分享
微信分享提示