JavaSE基础 - String常用API

本文详细介绍了JavaSE中关于字符串的一些基本操作,包括length()、charAt()、compareTo()、concat()、equals()、getBytes()、indexOf()、split()、substring()、toCharArray()、trim()和isEmpty()等方法,提供了示例代码和解释,帮助读者理解和掌握这些常用API的用法。
摘要由CSDN通过智能技术生成

前言

接下来我将会持续更新JavaSE基础系列的知识,希望小伙伴们多多支持!!!
📖系列专栏: JavaSE基础
先赞后看,已成习惯!!! 😃 😃 😃
你们的鼓励,就是我的动力!!! 💪 💪 💪
如果内容有误或是大家有好的建议欢迎大家在评论区提出,博主是会长期维护的哦!!! 🙋
如果大家觉得还不错,麻烦小伙伴们来一波免费的三连哦!!! 👍 👍 👍

常用API

方法名

说明

public char charAt(int index)

返回给定索引位置的对应的字符

public int compareTo(String anotherString)

比较字符串的大小

public String concat(String str)

字符串连接

public boolean equals(Object anObject)

比较字符串是否相等

public byte[] getBytes()

返回字节数组

public int indexOf(int ch)

查找给定字符的所在位置

public int length()

返回字符串长度

public String[] split(String regex)

拆分

substring(int beginIndex, int endIndex)

截取

public char[] toCharArray()

返回字符数组

trim()

去掉两端多余的空格

public boolean isEmpty()

判断是否为空串

  • length

// 1. 方法名:length 作用:可以返回给定字符串的长度
        String str = "abcdefg";
        System.out.println(str.length());
// ==================================================
// 运行结果:
7

Process finished with exit code 0
  • charAt

// 2. 方法名:charAt 作用:返回给定索引位置的对应字符
        String str = "abcdefg";
        System.out.println(str.charAt(0));
        System.out.println("================");
        // 通过该方法遍历一下该字符串
        for (int i = 0; i < str.length(); i++) {
            System.out.print(str.charAt(i) + " ");
        }
// ==================================================
// 运行结果:
a
================
a b c d e f g 
Process finished with exit code 0
  • compareTo

// 3. 方法名:compareTo 作用:比较两个字符串大小
        String str01 = "abcdefg";
        String str02 = "abcdefgh";

        System.out.println(str01.compareTo(str02)); // 输出结果:-1 表示 str01 < str02
// ==================================================================================
		String str01 = "abcdefgh";
        String str02 = "abcdefg";

        System.out.println(str01.compareTo(str02)); // 输出结果:1 表示 str01 > str02
// ==================================================================================
		String str01 = "abcdefg";
        String str02 = "abcdefg";

        System.out.println(str01.compareTo(str02)); // 输出结果:0 表示 str01 == str02
  • concat

// 4. 方法名:concat 作用:将两个字符串连接并返回
        String str = "Java";
        System.out.println(str.concat("SE"));
// ==============================================
// 运行结果:
JavaSE

Process finished with exit code 0
  • equals

// 5. 方法名:equals 作用:判断两个字符串是否相等,相等返回 true 不相等返回 false
        String str01 = "abcdefg";
        String str02 = "abcdefg";
        if (str01.equals(str02)){
            System.out.println("相等");
        }else {
            System.out.println("不相等");
        }
// ==============================================
// 运行结果:
相等

Process finished with exit code 0
    
// ===================================================================================
    	String str01 = "abcdefgh";
        String str02 = "abcdefg";
        if (str01.equals(str02)){
            System.out.println("相等");
        }else {
            System.out.println("不相等");
        }
// ==============================================
// 运行结果:
不相等

Process finished with exit code 0
  • getBytes

// 6. 方法名:getBytes 作用:将给定字符串转换成字节数组
        String str01 = "JavaSE";
        byte[] bytes = str01.getBytes();
        // 遍历数组
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i] + " ");
        }
// ==============================================
// 运行结果:
74 97 118 97 83 69 
Process finished with exit code 0
  • indexOf

// 7. 方法名:indexOf 作用:查找给定字符的所在位置(返回对应字符的下标)
        String str = "abcdefg";
        // 通过字符的 ASCII 码来查找
        System.out.println(str.indexOf(97));
        // 通过指定字符来查找
        System.out.println(str.indexOf("b"));
// ==============================================
// 运行结果:
0
1

Process finished with exit code 0
  • split

// 8. 方法名:split 作用:根据给定字符来拆分字符串,并将拆分完的每一部分以数组的形式返回
        String str = "JavaSE,MySQl,Linux";
        String[] arr = str.split(",");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
// ==============================================
// 运行结果:
JavaSE MySQl Linux 
Process finished with exit code 0
  • substring

// 9. 方法名:substring 作用:根据给定的下标范围来截取字符串
        String str = "abcdefg";
        // 注意:从索引为3开始截取,截取到6,但不包括下标6,实际是3到5
        String substring = str.substring(3, 6);
        System.out.println(substring);
// ==============================================
// 运行结果:
def

Process finished with exit code 0
  • toCharArray

// 10. 方法名:toCharArray 作用:将字符串转换成字符数组,并返回
        String str = "abcdefg";
        char[] arr = str.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
// ==============================================
// 运行结果:
a b c d e f g 
Process finished with exit code 0
  • trim

// 11. 方法名:trim 作用:去掉两端多余空格,并返回
        String str = " abcdefg ";
        System.out.println("(" + str.trim() + ")");
// ==============================================
// 运行结果:
(abcdefg)

Process finished with exit code 0
  • isEmpty

// 12. 方法名:isEmpty 作用:判断字符串是否为空,空返回 true 非空为 false
        String str = "";
        if (str.isEmpty()){
            System.out.println("字符串为空");
        }else {
            System.out.println("字符串非空");
        }
// ==============================================
// 运行结果:
字符串为空

Process finished with exit code 0
    
// ===================================================================================
    
   String str = "1";
        if (str.isEmpty()){
            System.out.println("字符串为空");
        }else {
            System.out.println("字符串非空");
        }
// ==============================================
// 运行结果:
字符串非空 
    
Process finished with exit code 0

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值