字符串替换和字符串拆分

字符串替换:

可以通过一个指定的内容进行指定字符串的替换显示;主要有两两个方法:

1、

public String replaceAll(String regex, String replacement);

进行全部替换

2、
public String replaceFirst(String regex, String replacement);
替换首个;

范例:

package 面向对象;

public class StringDemo02 {
    public static void main(String[] args) {
        String str = "helloworld";
        System.out.println(str.replaceAll("l", "X"));  //替换全部
        System.out.println(str.replaceFirst("l", "X"));  //替换首个
    }
}

输出结果:

heXXoworXd
heXloworld

在开发中,尤其是设计的时候替换很重要。

字符串拆分:

主要是可以根据一个指定的字符串或一些表达式实现字符串的拆分,并且拆分完成的数据将以字符串的形式返回。

1、public String[] split(String regex);

按照指定的字符串进行全部拆分;

2、public String[] split(String regex, int limit);

limit描述的是拆分的个数,按照指定的字符串拆分为指定个数,后面不拆了;

范例:观察字符串拆分处理

package 面向对象;

public class StringDemo02 {
    public static void main(String[] args) {
        String str = "hello world hello mldn";
        String[] res = str.split(" ");  //按空格拆分
        for (int x = 0; x < res.length; x++) {
            System.out.println(res[x]);
        }

    }
}

除了可以全部拆分之外,也可以拆分为指定的个数。

范例:拆分指定的个数

package 面向对象;

public class StringDemo02 {
    public static void main(String[] args) {
        String str = "hello world hello mldn";
        String[] res = str.split(" ", 2);  //按空格拆分
        for (int x = 0; x < res.length; x++) {
            System.out.println(res[x]);
        }

    }
}

输出结果:

hello
world hello mldn

但是在进行拆分的时候有可能会遇见拆不了的情况,这个时候最简单的理解就是使用"\"进行转义。

package 面向对象;

public class StringDemo02 {
    public static void main(String[] args) {
        String str = "hello world hello mldn";
        String[] res = str.split(" ", 2);  //按空格拆分
        for (int x = 0; x < res.length; x++) {
            System.out.println(res[x]);
        }

        String strA = "192.168.23";
        String[] resA = strA.split("\\.");  //用了转义字符
        for (int i = 0; i < resA.length; i++) {
            System.out.println(resA[i]);
        }

    }
}

字符串截取:

从一个完整的字符串中截取出子字符串,有两个方法:

1、public String substring(int beginIndex);

从指定索引截取到结束

2、public String substring(int beginIndex, int endIndex);

截取指定索引范围中的子字符串

范例:观察字符串截取操作

package 面向对象;

public class StringDemo02 {
    public static void main(String[] args) {
      String str = "www.mldn.cn";
        System.out.println(str.substring(4));  //mldn.cn
        System.out.println(str.substring(4, 8));  //mldn

    }
}

在实际开发中,有些时候的开始或结束索引往往是通过indexOf()方法计算得来的。

范例:观察截取字符串

package 面向对象;

public class StringDemo02 {
    public static void main(String[] args) {

        //字符串结构:"mldn-photo-姓名.jpg"
        String str = "mldn-photo-张三.jpg";

        //找到以photo为开始点的'-'的索引位置,然后再加1
        int beginIndex = str.indexOf("-", str.indexOf("photo")) + 1;
        int endIndex = str.lastIndexOf(".");
        System.out.println(str.substring(beginIndex, endIndex));
    }
}

输出结果:张三

在以后实际开发中,这种通过计算来确定索引的情况很常见

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 字符串 String表示字符串类型,String虽然本身不属于基本数据类型,但是可以像基本数据类型一样使用方便,是...
    BP丶阅读 216评论 0 0
  • 1.字符串的替换·public String replaceAll(String regex, String re...
    妃雪阅读 200评论 0 0
  • package cn.itcast_01;/* 字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。 ...
    蛋炒饭_By阅读 613评论 0 0
  • 槐花赞 看到河岸上雪白的槐花,在翠绿的树叶的映衬下,显得秀丽、纯洁,芬芳...
    晓英写事阅读 501评论 5 17
  • 最近1年非常关注个人成长的内容,已经38岁了,不知道之前自己是什么浑浑噩噩过来的,到了这个年龄,自己的成长、...
    青苗妈2016阅读 112评论 0 0