String类(续)
- 按字典顺序比较两个字符串,用compareTo(String otherstr),只有两个字符串equals(Object)相等时才返回0,否则大于就返回大于0的值,小于就返回小于0的值。
- 字符串大小写转换,大转小:toLowerCase(),小转大:toUpperCase()。
- 字符串分割,str.split(sign),sign为分隔符(多个分隔符用|分隔开,例如,",|="表示分隔符为“,”和“=”号),两个参数时,第二个表示分割次数
public class String2 { public static void main(String[] args) { String firstStr = "abcdefg" ; String secondStr = "abcdefg"; String thirdStr = "abcdefG"; System.out.println(firstStr.compareTo(secondStr)); //0 System.out.println(firstStr.compareTo(thirdStr)); //32 System.out.println(firstStr.toLowerCase()); //abcdefg System.out.println(firstStr.toUpperCase()); //ABCDEFG String[] allarr = firstStr.split(""); String[] twoarr = firstStr.split("",2); for(String x:allarr) { System.out.print(x+" "); // a b c d e f g } System.out.print("\n"); for(String x:twoarr) { System.out.print(x+" "); // a bcdefg } } }五、格式化字符串
方法
- str.format(String format,Object…args)
- str.format(Local l, String format,Object…args)
日期格式化
- 常用日期时间格式化
使用示例:
import java.util.Date; public class Formatdate { public static void main(String[] args) { Date date = new Date(); String year = String.format("%tY", date); String month = String.format("%tB", date); String day = String.format("%td", date); System.out.println("今年是"+year+"年"); System.out.println("是"+month); System.out.println("今天是"+day+"号"); } }常规类型格式化
使用示例:
public class Formatdata { public static void main(String[] args) { String str = String.format("%d", 400/2); //整数 String str2 = String.format("%b", 3>5); //boolean String str3 = String.format("%x", 200); // 十六进制 System.out.println(str); //200 System.out.println(str2); //false System.out.println(str3); //c8 } }📦 前端资源合集 | 持续更新
🟢 前端0到1【持续更新】→ https://pan.quark.cn/s/5df55ccff7c4
🔵 前端进阶【持续更新】→ https://pan.quark.cn/s/2dec1c87b3ec
🟣 前端2026最新【持续更新】→ https://pan.quark.cn/s/77c8fa94161c
🔴 AI最新学习资料 → https://pan.baidu.com/s/1P9X2Qk_Fby3rFNVGw_WKow?pwd=46XG 提取码:46XG
觉得有用就点个赞+收藏,关注我持续分享前端干货 💡