总结并拆解所有新手常用的——String API(一)

前言:

String 类包括的方法可用于检查序列的单个字符、比较字符串、搜索字符串、 提取子字符串、创建字符串副本并将所有字符全部转换为大写或小写.......

小编这次就比较全面系统的带大家总结清楚几乎所有string常用的API,并且带大家拆解清楚,能够灵活使用!!!

小编今天总算是回家了

日更一篇,今天这篇一定会对小白非常有用的!!!

因为我们会把案例到用代码实现的全过程思路呈现出来!!!

我们一直都是以这样的形式,让新手小白轻松理解复杂晦涩的概念,

把Java代码拆解的清清楚楚,每一步都知道他是怎么来的,

为什么用这串代码关键字,对比同类型的代码,

让大家真正看完以后融会贯通,举一反三,实践应用!!!!


①官方定义  和  大白话拆解对比

②举生活中常见贴合例子、图解辅助理解的形式

③对代码实例中关键部分进行详细拆解、总结



给小编一个赞或者关注吧,我们一起进步!!

1:常用方法

  • (1)boolean isEmpty():字符串是否为空
  • (2)int length():返回字符串的长 度
  • (3)String concat(xx):拼接
  • (4)boolean equals(Object obj):比较字符 串是否相等,区分大小写
  • (5)boolean equalsIgnoreCase(Object obj):比较字 符串是否相等,不区分大小写
  • (6)int compareTo(String other):比较字符串 大小,区分大小写,按照Unicode编码值比较大小 (7)int compareToIgnoreCase(String other):比较字符串大小,不区分大小写
  • (8) String toLowerCase():将字符串中大写字母转为小写
  • (9)String toUpperCase():将字符串中小写字母转为大写
  • (10)String trim():去掉字符 串前后空白符
  • (11)public String intern():结果在常量池中共享

1. boolean isEmpty()


官方语言:
返回值为true如果字符串长度为0(即它不包含任何字符),否则返回false。


大白话拆解:
这个方法用来检查一个字符串是不是空的,就是说里面有没有任何文字。如果有任何文字或符号,就返回false;如果是完全空的,就返回true。

举个栗子:

String str = "";
System.out.println(str.isEmpty()); // 输出: true

str = "Hello";
System.out.println(str.isEmpty()); // 输出: false

2. int length()


官方语言:
返回此字符串的长度,即字符串中字符的数量。


大白话拆解:
这个方法用来数一数字符串里有多少个字符,包括字母、数字、符号和空格。

举个栗子:

String str = "Hello";
System.out.println(str.length()); // 输出: 5

3. String concat(String str)


官方语言:
将指定的字符串连接到该字符串的末尾,并返回新的字符串。


大白话拆解:
这个方法可以把两个字符串粘在一起,生成一个新的字符串。

举个栗子:

String str1 = "Hello, ";
String str2 = "World!";
String result = str1.concat(str2);
System.out.println(result); // 输出: Hello, World!

4. boolean equals(Object obj)


官方语言:
比较此字符串与给定的对象是否相等,区分大小写。只有当给定对象也是一个字符串并且内容完全相同的时候才返回true。


大白话拆解:
这个方法用来判断两个字符串是不是一模一样,包括大小写也要完全一致。

举个栗子:

String str1 = "hello";
String str2 = "Hello";
System.out.println(str1.equals(str2)); // 输出: false

5. boolean equalsIgnoreCase(Object obj)


官方语言:
比较此字符串与给定的对象是否相等,不区分大小写。


大白话拆解:
这个方法也是用来判断两个字符串是不是一样,但是它不在乎字母是大写还是小写。

举个栗子:

String str1 = "hello";
String str2 = "Hello";
System.out.println(str1.equalsIgnoreCase(str2)); // 输出: true

6. int compareTo(String other)


官方语言:
按字典顺序比较两个字符串,区分大小写。根据Unicode编码值进行比较,返回一个整数值表示比较结果。


大白话拆解:
这个方法是用来比较两个字符串的“字典顺序”,会告诉你哪个应该排在前面。它会考虑大小写,大写字母通常被认为是在小写字母之前。

举个栗子:

String str1 = "apple";
String str2 = "banana";
System.out.println(str1.compareTo(str2)); // 输出: 负数(因为"apple"在"banana"之前)

7. int compareToIgnoreCase(String other)


官方语言:
按字典顺序比较两个字符串,不区分大小写。


大白话拆解:
这个方法和compareTo类似,但不会考虑字母的大小写。

举个栗子:

String str1 = "Apple";
String str2 = "banana";
System.out.println(str1.compareToIgnoreCase(str2)); // 输出: 负数(忽略大小写后,"Apple"仍在"banana"之前)

8. String toLowerCase()


官方语言:
将所有大写字母转换为对应的小写字母,其他字符保持不变。


小白语言:
这个方法可以把字符串里的大写字母都变成小写字母,其他字符不变。

举个栗子:

String str = "HeLlO";
System.out.println(str.toLowerCase()); // 输出: hello

9. String toUpperCase()


官方语言:
将所有小写字母转换为对应的大写字母,其他字符保持不变。


小白语言:
这个方法可以把字符串里的小写字母都变成大写字母,其他字符不变。

举个栗子:

String str = "HeLlO";
System.out.println(str.toUpperCase()); // 输出: HELLO

10. String trim()


官方语言:
返回去掉字符串开头和结尾空白字符的新字符串。


小白语言:
这个方法可以去除字符串前后的多余空格。

举个栗子:

String str = "   Hello, World!   ";
System.out.println(str.trim()); // 输出: "Hello, World!"

11. public String intern()


官方语言:
返回字符串的规范实例。对于所有具有相同字符序列的字符串,intern方法将返回相同的字符串实例。


小白语言:
这个方法会确保如果你有多个相同的字符串,它们实际上指向同一个地方,节省内存空间。

举个栗子:

String str1 = new String("hello");
String str2 = new String("hello");
String str3 = str1.intern();
System.out.println(str1 == str2); // 输出: false (不同的对象)
System.out.println(str1 == str3); // 输出: true (共享相同的内部池对象)

12、举一个综合代码例子!!!

public class StringMethodsExample {
    public static void main(String[] args) {
        // 定义一个空字符串和非空字符串
        String emptyStr = "";
        String nonEmptyStr = "Hello, World!";
        
        // (1) 检查字符串是否为空
        System.out.println("Is the string empty? " + emptyStr.isEmpty()); // 输出: true
        System.out.println("Is the string not empty? " + !nonEmptyStr.isEmpty()); // 输出: true
        
        // (2) 获取字符串长度
        System.out.println("Length of non-empty string: " + nonEmptyStr.length()); // 输出: 13
        
        // (3) 字符串拼接
        String part1 = "Hello";
        String part2 = "World";
        String combined = part1.concat(" ").concat(part2); // 把两个字符串用空格连接起来
        System.out.println("Concatenated string: " + combined); // 输出: Hello World
        
        // (4) 比较字符串是否相等(区分大小写)
        String strA = "hello";
        String strB = "Hello";
        System.out.println("Are they equal (case-sensitive)? " + strA.equals(strB)); // 输出: false
        
        // (5) 比较字符串是否相等(不区分大小写)
        System.out.println("Are they equal (ignore case)? " + strA.equalsIgnoreCase(strB)); // 输出: true
        
        // (6) 按照Unicode编码值比较字符串大小(区分大小写)
        String strC = "apple";
        String strD = "banana";
        int comparison = strC.compareTo(strD);
        System.out.println("Comparison result (case-sensitive): " + comparison); // 输出: 负数,表示strC在字典顺序上排在strD之前
        
        // (7) 按照Unicode编码值比较字符串大小(不区分大小写)
        comparison = strC.compareToIgnoreCase(strD);
        System.out.println("Comparison result (ignore case): " + comparison); // 输出: 同上,因为都是小写
        
        // (8) 将字符串中大写字母转为小写
        String upperCaseStr = "HELLO";
        System.out.println("Lowercase version: " + upperCaseStr.toLowerCase()); // 输出: hello
        
        // (9) 将字符串中小写字母转为大写
        String lowerCaseStr = "world";
        System.out.println("Uppercase version: " + lowerCaseStr.toUpperCase()); // 输出: WORLD
        
        // (10) 去掉字符串前后空白符
        String withSpaces = "   Trim this string   ";
        System.out.println("Trimmed string: '" + withSpaces.trim() + "'"); // 输出: 'Trim this string',注意引号显示去掉的空格
        
        // (11) 结果在常量池中共享
        String internedStr = new String("intern");
        String anotherInternedStr = "intern"; // 直接赋值的字符串自动进入常量池
        String internedAgain = internedStr.intern(); // 强制将字符串放入常量池并返回常量池中的实例
        
        // 使用 == 操作符检查它们是否指向同一个对象
        System.out.println("Do they refer to the same object in the pool? " + (internedStr == anotherInternedStr)); // 输出: false
        System.out.println("Do they refer to the same object in the pool after interning? " + (internedAgain == anotherInternedStr)); // 输出: true
    }
}

13、综合代码的解释和总结:

  • (1) isEmpty() 方法用于检查字符串是否为空。如果字符串里没有任何字符,那么这个方法会返回true。我们用这个方法分别检查了emptyStr和nonEmptyStr,其中emptyStr是空的,所以返回true;而nonEmptyStr不是空的,所以返回false。

  • (2) length() 方法用来获取字符串的长度,即字符串中字符的数量。我们使用nonEmptyStr.length()来得到"Hello, World!"的长度,结果是13个字符。

  • (3) concat() 方法允许我们将两个字符串连接在一起。这里我们用了两次concat()方法,先添加了一个空格,然后把part2加到了part1后面,最终得到了"Hello World"。

  • (4) 和 (5) equals() 以及 equalsIgnoreCase() 方法是用来比较两个字符串是否相同。equals()方法是区分大小写的,而equalsIgnoreCase()则不区分。我们通过比较strA和strB展示了这一点,equals()返回false,而equalsIgnoreCase()返回true,因为忽略大小写后它们是一样的。

  • (6) 和 (7) compareTo() 与 compareToIgnoreCase() 方法用于按字典顺序比较两个字符串。compareTo()方法考虑大小写,而compareToIgnoreCase()则不考虑。这些方法返回一个整数值,如果前一个字符串小于、等于或大于后一个字符串,则分别返回负数、零或正数。

  • (8) 和 (9) toLowerCase() 和 toUpperCase() 方法用于转换字符串的大小写。toLowerCase()会将所有大写字母变成小写,而toUpperCase()则是将所有小写字母变成大写。

  • (10) trim() 方法可以去除字符串开头和结尾的空白字符。我们用withSpaces.trim()去掉了字符串两端的多余空格,输出时可以看到结果没有了外面的空格。

  • (11) intern() 方法确保对于具有相同字符序列的字符串,在内存中只保留一份副本。当调用intern()方法时,如果常量池中已经存在相同的字符串,它就会返回那个字符串的引用;否则,它会将当前字符串放入常量池,并返回该池中的引用。在这个例子中,internedStr和anotherInternedStr虽然内容相同,但最初不是同一个对象,因此==操作符返回false。然而,调用internedStr.intern()之后,它返回的是常量池中的实例,此时internedAgain和anotherInternedStr指向了同一个对象,所以==操作符返回true。



小编今天就先给大家总结到这里,有帮助的话,给小编一个关注吧!!!

我们一起进步!!!



2:查找

3:字符串截取

4:和字符/字符数组相关

5:开头与结尾

6:替换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值