Java字符串String的定查改增删操作

定义

字符串的定义包括声明和初始化两个过程。

声明

声明,就是向编译器说明新变量的类型和名字,不实际分配内存。

String str;

上面这句,说明新变量的数据类型是String,变量名是str。

初始化

声明的同时初始化

可以在声明str的同时,使用new命令和字符串常量/char型的数组对str进行初始化。

String str = new String("hello");

char[] array = new char[] { 'h', 'e', 'l', 'l', 'o'};
String str = new String(array);

也可以省略new命令,直接使用字符串常量对str进行初始化。

String str = "hello";

但不可以省略new命令,直接使用char型的数组对str进行初始化!!!以下是错误示范!!!

char[] array = new char[] { 'h', 'e', 'l', 'l', 'o'};
String str = array;

因为str是String类型的引用型变量,而array是char类型的数组,所以两者搭配不上。

先声明,再初始化

如果先声明,那么可以采用以下三种方式进行初始化。

String str;
// 方式一
str = "hello";
// 方式二
str = new String("hello");
// 方式三
char[] array = new char[] { 'h', 'e', 'l', 'l', 'o'};
str = new String(array);

查找

String类提供了更加丰富的函数供我们使用。

如果你想查找字符串中某个位置的元素是什么,可以使用类似数组下标的方式。

public class Main
{
    public static void main(String[] args) {
        String str = "hello";
        int n = str.length();
        for(int i = 0; i < n; i++) {
        	System.out.println(str.charAt(i));
        }
    }
}

可以看到,对于String来说,这些操作都被包装成了函数。

如果你想查找某个元素在字符串中的位置,可以先判断这个元素是否存在,然后再查找位置。

public class Main
{
    public static void main(String[] args) {
        String str = "hello";
        String key = "l";
        if(str.contains(key)){
        	System.out.println("the first key on " + str.indexOf(key));
        	System.out.println("the last key on " + str.lastIndexOf(key));
        }
    }
}

修改

实际上,String类是无法修改的。String类提供的方法,实际上是返回一个新字符串,其值为修改后的值。

你可以将字符串中的某些元素,替换成其他元素。

public class Main
{
    public static void main(String[] args) {
        String str = "hello";
        String key = "l";
        if(str.contains(key)){
            String new_str = str.replace(key, "k");
        	System.out.println("the old str is " + str);
        	System.out.println("the new str is " + new_str);
        }
    }
}

增加&删除

如前文所说,String类是无法修改的,只不过返回一个新的字符串。想要实现真正的插入和删除,使用StringBuilder类。

但是,String类还是提供了一些对于子串的操作。

截取子串

你可以将字符串中的某些元素,截取为子串。

public class Main
{
    public static void main(String[] args) {
        String str = "hello";
        String key = "l";
        if(str.contains(key)){
        	System.out.println("the first key on " + str.indexOf(key));
        	System.out.println("the last key on " + str.lastIndexOf(key));
        	String new_str = str.substring(str.indexOf(key), str.lastIndexOf(key) + 1);
        	// 左闭右开
        	System.out.println("the new str is " + new_str);
        }
    }
}

分割子串

可以将字符串中的某个字符作为分隔符,将字符串分割为多个子串。

public class Main
{
    public static void main(String[] args) {
        String str = "h_e_l_l_o";
        String[] strs = str.split("_");
        for(String item : strs){
            System.out.println(item);
        }
    }
}

拼接

上面分割子串的反操作,用某个字符作为连接符,将多个子串连接为一个字符串。

public class Main
{
    public static void main(String[] args) {
        String[] strs = {"h", "e", "l", "l", "o"};
        String str = String.join("_", strs);
        System.out.println(str);
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值