【04】Java入门04:常用API基础
一、String类
1.String类概述
①"abc"是String类的一个实例,或者成为String类的一个对象。
②字符串字面值"abc"也可以看成是一个字符串对象。
③字符串是常量,一旦被赋值,就不能被改变。
④字符串本质是一个字符数组 。
2.String类的构造方法
①String(String original):把字符串数据封装成字符串对象。
②String(char[] value):把字符数组的数据封装成字符串对象。
③String(char[] value, int index, int count):把字符数组中的一部分数据封装成字符串对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class StringDemo { public static void main(String[] args) { // 方式1 // String(String original):把字符串数据封装成字符串对象 String s1 = new String( "hello" ); System.out.println( "s1:" +s1); System.out.println( "---------" ); // 方式2 // String(char[] value):把字符数组的数据封装成字符串对象 char [] chs = { 'h' , 'e' , 'l' , 'l' , 'o' }; String s2 = new String(chs); System.out.println( "s2:" +s2); System.out.println( "---------" ); // 方式3 // String(char[] value, int index, int count):把字符数组中的一部分数据封装成字符串对象 // String s3 = new String(chs,0,chs.length); String s3 = new String(chs, 1 , 3 ); System.out.println( "s3:" +s3); System.out.println( "---------" ); // 方式4 String s4 = "hello" ; System.out.println( "s4:" +s4); } } |
3.String类的判断功能
①boolean equals(Object obj):比较字符串的内容是否相同。
②boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写。
③boolean startsWith(String str):判断字符串对象是否以指定的str开头。
④boolean endsWith(String str):判断字符串对象是否以指定的str结尾。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class StringDemo { public static void main(String[] args) { //创建字符串对象 String s1 = "hello" ; String s2 = "hello" ; String s3 = "Hello" ; //boolean equals(Object obj):比较字符串的内容是否相同 System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println( "-----------" ); //boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 System.out.println(s1.equalsIgnoreCase(s2)); System.out.println(s1.equalsIgnoreCase(s3)); System.out.println( "-----------" ); //boolean startsWith(String str):判断字符串对象是否以指定的str开头 System.out.println(s1.startsWith( "he" )); System.out.println(s1.startsWith( "ll" )); } } |
4.String类的获取功能
①int length():获取字符串的长度,其实也就是字符个数。
②char charAt(int index):获取指定索引处的字符。
③int indexOf(String str):获取str在字符串对象中第一次出现的索引。
④String substring(int start):从start开始截取字符串。
⑤String substring(int start,int end):从start开始,到end结束截取字符串。包括start,不包括end。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class StringDemo { public static void main(String[] args) { //创建字符串对象 String s = "helloworld" ; //int length():获取字符串的长度,其实也就是字符个数 System.out.println(s.length()); System.out.println( "--------" ); //char charAt(int index):获取指定索引处的字符 System.out.println(s.charAt( 0 )); System.out.println(s.charAt( 1 )); System.out.println( "--------" ); //int indexOf(String str):获取str在字符串对象中第一次出现的索引 System.out.println(s.indexOf( "l" )); System.out.println(s.indexOf( "owo" )); System.out.println(s.indexOf( "ak" )); System.out.println( "--------" ); //String substring(int start):从start开始截取字符串 System.out.println(s.substring( 0 )); System.out.println(s.substring( 5 )); System.out.println( "--------" ); //String substring(int start,int end):从start开始,到end结束截取字符串 System.out.println(s.substring( 0 , s.length())); System.out.println(s.substring( 3 , 8 )); } } |
5.String类的转换功能
①char[] toCharArray():把字符串转换为字符数组。
②String toLowerCase():把字符串转换为小写字符串。
③String toUpperCase():把字符串转换为大写字符串。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class StringDemo { public static void main(String[] args) { //创建字符串对象 String s = "abcde" ; //char[] toCharArray():把字符串转换为字符数组 char [] chs = s.toCharArray(); for ( int x= 0 ; x<chs.length; x++) { System.out.println(chs[x]); } System.out.println( "-----------" ); //String toLowerCase():把字符串转换为小写字符串 System.out.println( "HelloWorld" .toLowerCase()); //String toUpperCase():把字符串转换为大写字符串 System.out.println( "HelloWorld" .toUpperCase()); } } |
6.String类的其它功能
①去除字符串两端空格:String trim()
②按照指定符号分割字符串:String[] split(String str)
③将字符串倒序输出:reverse(String)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class StringDemo { public static void main(String[] args) { // 创建字符串对象 String s1 = "helloworld" ; String s2 = "helloworld" ; String s3 = "hello world" ; System.out.println( "---" +s1+ "---" ); System.out.println( "---" +s1.trim()+ "---" ); System.out.println( "---" +s2+ "---" ); System.out.println( "---" +s2.trim()+ "---" ); System.out.println( "---" +s3+ "---" ); System.out.println( "---" +s3.trim()+ "---" ); System.out.println( "-------------------" ); String result = reverse(s1); // 输出结果 System.out.println( "result:" +result); // String[] split(String str) // 创建字符串对象 String s4 = "aa,bb,cc" ; String[] strArray = s4.split( "," ); for ( int x= 0 ; x<strArray.length; x++) { System.out.println(strArray[x]); } } } |
二、StringBuilder类
1.StringBuilder类概述
1) StringBuilder
是一个可变的字符串,字符串缓冲区类。
2) String和StringBuilder的区别
①String的内容是固定的。
②StringBuilder的内容是可变的。
2.+=拼接字符串耗费内存原因
每次拼接都会产生新的字符串对象,而利用StringBuilder来拼接字符串自始至终用的都是同一个StringBuilder容器。
3.StringBuilder类的常用方法
1) 构造方法
①StringBuilder() 。
2) 成员方法
①public int capacity():返回当前容量 (理论值)。
②public int length():返回长度(已经存储的字符个数)。
③public StringBuilder append(任意类型):添加数据,并返回自身对象。
④public StringBuilder reverse():反转功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class StringBuilderDemo { public static void main(String[] args) { //创建对象 StringBuilder sb = new StringBuilder(); System.out.println( "sb:" +sb); System.out.println( "sb.capacity():" +sb.capacity()); System.out.println( "sb.length():" +sb.length()); //链式编程 sb.append( "hello" ).append( "world" ).append( true ).append( 100 ); System.out.println( "sb:" +sb); //public StringBuilder reverse() sb.reverse(); System.out.println( "sb:" +sb); } } |
4.StringBuilder和String通过方法完成相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class StringBuilderTest { public static void main(String[] args) { //StringBuilder -- String /* StringBuilder sb = new StringBuilder(); sb.append("hello").append("world"); String s = sb.toString(); System.out.println(s); */ //String -- StringBuilder String s = "helloworld" ; StringBuilder sb = new StringBuilder(s); System.out.println(sb); } } |
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 我在厂里搞 wine 的日子
· 如何通过向量化技术比较两段文本是否相似?
· 35+程序员的转型之路:经济寒冬中的希望与策略
· JavaScript中如何遍历对象?
· 领域模型应用
· 独立项目运营一周年经验分享
· 独立开发,这条路可行吗?
· 文生图:介绍一个文字生成图片的开源工具
· Java简历、面试、试用期、转正
· MySQL 10 MySQL为什么有时候会选错索引?