package cn.lgt.stringmethods;
public class StringOtherMethods {
public static void stringOtherMehtods(){
//String类的其他功能
//替换功能
//1. String replace(char old, char new);
//2. String replace(String old, String new);
String s1 = "helloworld";
System.out.println(s1.replace('o', 'm'));
System.out.println(s1.replace("world", "java"));
//去除字符串两端的空格
//String trim();
String s2 = " dd sss d ";
System.out.println(s2.trim());
//按照字典顺序比较两个字符串
//int compareTo(String str);//区分大小写
//int compareToIgnoreCase(String str);//不区分大小写
String s3 = "helloworld";
String s4 = "HELLOWORLD";
String s5 = "hello";
String s6 = "world";
System.out.println(s3.compareTo(s5));//前面都相同,返回相差的字符数
System.out.println(s3.compareToIgnoreCase(s4));//不区分大小写,相同,返回数值0
System.out.println(s3.compareTo(s6));
}
}
String类的其他功能replace、compareTo
最新推荐文章于 2021-07-12 18:15:06 发布
本文详细介绍了 Java 中 String 类的一些实用方法,包括字符串替换、去除两端空格、比较字符串等基本操作,并通过示例代码展示了这些方法的具体使用方式。
715

被折叠的 条评论
为什么被折叠?



