java字符串操作

1、字符串拼接

直接使用加号拼接即可

public class Main {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "World";
        // 字符串拼接
        String str3 = str1 + str2;
        System.out.println("str3=" + str3); // str3=HelloWorld
        
    }
}

2、获取字符串长度

length方法获取长度

public class demo {
    public static void main(String[] args) {
        String str3 = "hello";
         // 获取字符串长度
        int length = str3.length();
        System.out.println(length); // 5
    }
}

3、 字符串比较

equals方法判断两个字符串的值是否相等
contains方法判断字符串1中是否包含字符串2
两种方法均返回boolean

public class demo {
    public static void main(String[] args) {
        String str3 = "hello";
        String str5 = "hello1";
         // 字符串比较
        boolean isEqual = str3.equals(str5); // isEqual = false
        System.out.println(isEqual);

        // 判断字符串中是否包含指定字符串
        boolean contains = str3.contains("llo"); // contains = true
        System.out.println(contains);
    }
}

4、字符串替换

replace方法

public class demo {
    public static void main(String[] args) {
        String str3 = "hello world";
        String str5 = "hello1";
         // 字符串替换
        String replaced = str3.replace("world", str5);
        System.out.println(replaced);  //hello hello1
    }
}

5. 大小写转换

toLowerCase方法将字符串全部转换为小写
toUpperCase方法将字符串全部转换为小写

public class demo{
    public static void main(String[] args) {
        String str1 = "Hello";
        // 字符串转为全部小写
        String lowerStr = str1.toLowerCase(); // lowerStr = "hello world"
        // 字符串转为全部大写
        String upperStr = str1.toUpperCase(); // upperStr = "HELLO WORLD"
    }
}

6.字符串切片截取

substring方法对字符串进行切片

public class demo{
    public static void main(String[] args) {
        String str4 = "Hello World";
        // 字符串切片截取 如果只传一个参数,代表开始下标
        String subStr = str4.substring(6); // subStr = "World"
        String subStrRange = str4.substring(0, 5); // subStrRange = "Hello"
        System.out.println("subStr:" + subStr + " " + "subStrRange:" + subStrRange);

    }
}

7. 去除字符串两端空格

trim方法去除字符串两端空格

public class demo{
    public static void main(String[] args) {
        String str = " Hello World ";
        // 去除两端空格
        String trimmed = str.trim(); // trimmed = "Hello World"
    }
}

8. 字符串切割为列表

split方法将字符串切割为列表

public class demo{
    public static void main(String[] args) {
        // 字符串切割
        String str6 = "apple,banana,cherry";
        // 使用逗号作为分隔符进行切割
        String[] fruits = str6.split(",");
        System.out.println(Arrays.toString(fruits)); // 输出 [apple, banana, cherry]
        String str7 = "apple banana cherry";
        // 使用空格作为分隔符进行切割
        String[] fruits1 = str7.split(" ");
        System.out.println(Arrays.toString(fruits1)); // 输出 [apple, banana, cherry]
    }
}

9. 判断是否以指定字符串开头或结尾

startsWith方法判断是否以指定字符串开头
endsWith方法判断是否以指定字符串结尾
二者均返回boolean 类型

public class Lianxi1 {
    public static void main(String[] args) {
        String str = "hello world";
        // 检查字符串是否以"Hello"开头
        boolean result1 = str.startsWith("Hello");
        System.out.println(result1); // 输出 false

        // 检查字符串是否以"world"结尾(注意大小写)
        boolean result2 = str.endsWith("world");
        System.out.println(result2); // 输出 true
    }
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kopokliv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值