2020年9月17日 String 常用方法四、五、六、七、八、九

package com.atguigu.test09;

import org.junit.Test;

/*
 * 方法系列(4):
 * (1)boolean startsWith(xx)
 * (2)boolean endsWith(xx)
 */
public class TestStringMethod4 {
    @Test
    public void test3(){
        String fileName = "Hello.class";
        if(fileName.endsWith(".java")){
            System.out.println("Java的源文件");
        }else if(fileName.endsWith(".class")){
            System.out.println("字节码文件");
        }
    }
    
    @Test
    public void test2(){
        String str = "尚硅谷是一家靠谱的培训机构";
        System.out.println(str.startsWith("尚硅谷"));
    }
    
    @Test
    public void test1(){
        String name = "崔志恒";
        if(name.startsWith("张")){
            System.out.println("张家人");
        }else{
            System.out.println("不是张家人");
        }
    }
}
package com.atguigu.test09;

import org.junit.Test;

/*
 * 方法系列(5):和查找有关
 * (1)是否包含
 *    boolean contains
 * (2)int indexOf(xx):如果存在返回下标,如果不存在返回-1
 * (3)int lastIndexOf(xx):如果存在返回最后一个指定目标(xx)的下标,如果不存在返回-1
 */
public class TestStringMethod5 {
    @Test
    public void test03(){
        String fileName = "Hello.java.txt";
        //文件的后缀名是什么
        //截取文件的后缀名
        //(1)第一步,找到最后一个.的位置
        int index = fileName.lastIndexOf(".");
        System.out.println(index);
    }
    
    @Test
    public void test02(){
        String str = "123.45";
//        String str = "123";
        int index = str.indexOf(".");
        System.out.println(index);
    }
    
    @Test
    public void test01(){
        String str = "123.45";
        
        if(str.contains(".")){
            System.out.println("是小数");
        }
    }
}
package com.atguigu.test09;

import org.junit.Test;

/*
 * 方法系列(6):截取
 * (1)String substring(int beginIndex):从字符串的[beginIndex]截取到最后
 * (2)String substring(int beginIndex, int endIndex):截取字符串的[beginIndex,endIndex)部分
 */
public class TestMethod6 {
    @Test
    public void test04(){
        String str = "helloworldjava";
        String sub = str.substring(2, 6);
        System.out.println(sub);
    }
    
    @Test
    public void test03(){
        String fileName = "Hello.java.txt";
        //文件的后缀名是什么
        //截取文件的后缀名
        //(1)第一步,找到最后一个.的位置
        int index = fileName.lastIndexOf(".");

        //(2)截取
        String sub = fileName.substring(index);
        System.out.println(sub);
    }
}
package com.atguigu.test09;

import org.junit.Test;

/*
 * 方法系列(7):匹配规则
 * boolean matches(正则表达式)
 * 
 * 正则表达式:用于检测文本的格式
 * 校验某个字符串是否符合xx规则
 * 例如:电话号码
 *         甚至可以校验是否是移动号...
 *         银行卡号
 *           邮箱格式
 *         ....
 * 
 * 
 */
public class TestMethod7 {
    @Test
    public void test2(){
        String str = "12a345";
        //简单判断是否全部是数字,这个数字可以是1~n位
        
        //正则不是Java的语法,它是独立与Java的规则
        //在正则中\是表示转义,
        //同时在Java中\也是转义
        boolean flag = str.matches("\\d+");
        System.out.println(flag);
    }
    
    @Test
    public void test1(){
        String str = "123456789";
        
        //判断它是否全部由数字组成,并且第1位不能是0,长度为9位
        //第一位不能是0,那么数字[1-9]
        //接下来8位的数字,那么[0-9]{8}+
        boolean flag = str.matches("[1-9][0-9]{8}+");
        System.out.println(flag);
    }
}
package com.atguigu.test09;

import org.junit.Test;

/*
 * 方法系列(8):替换
 * (1)String replace(target, value)
 * (2)String replaceAll(String regex, String replacement)
 * (3)String replaceFirst(String regex, String replacement)
 * 其中(2)和(3)支持正则
 */
public class TestMethod8 {
    
    @Test
    public void test4(){
        String str = "hello244world.java;887";
        //把其中的非字母去掉
        str = str.replaceAll("[^a-zA-Z]", "");
        System.out.println(str);
    }
    @Test
    public void test3(){
        String str = "中国人民是伟大的,中国人民是伟大的人民";
        str = str.replaceAll("人民", "***");//相比于replace,支持正则
        System.out.println(str);
    }
    
    @Test
    public void test2(){
        String str = "中国人民是伟大的,中国人民是伟大的人民";
        str = str.replaceFirst("人民", "***");//相比于replace,支持正则
        System.out.println(str);
    }
    
    
    @Test
    public void test1(){
        String str = "中国人民是伟大的,中国人民是伟大的人民";
        str = str.replace("人民", "***");
        System.out.println(str);
    }
}
package com.atguigu.test09;

import java.util.Arrays;

import org.junit.Test;

/*
 * 方法系列(9):拆分
 * String[] split(xx)
 */
public class TestMethod9 {
    @Test
    public void test4(){
        String str = "张三.23|李四.24|王五.25";
        //|在正则中是有特殊意义,我这里要把它当做普通的|
        String[] all = str.split("\\|");
        
        //转成一个一个学生对象
        Student[] students = new Student[all.length];
        for (int i = 0; i < students.length; i++) {
            //.在正则中是特殊意义,我这里想要表示普通的.
            String[] strings = all[i].split("\\.");//张三,  23
            String name = strings[0];
            int age = Integer.parseInt(strings[1]);
            students[i] = new Student(name,age);
        }
        
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i]);
        }
        
    }
    
    @Test
    public void test3(){
        String str = "1Hello2World3java4atguigu5";
        str = str.replaceAll("^\\d|\\d$", "");
        String[] all = str.split("\\d");
        for (int i = 0; i < all.length; i++) {
            System.out.println(all[i]);
        }
    }
    
    @Test
    public void test2(){
        String str = "1Hello2World3java4atguigu";
        str = str.replaceFirst("\\d", "");
        System.out.println(str);
        String[] all = str.split("\\d");
        for (int i = 0; i < all.length; i++) {
            System.out.println(all[i]);
        }
    }
    
    
    @Test
    public void test1(){
        String str = "Hello World java atguigu";
        String[] all = str.split(" ");
        for (int i = 0; i < all.length; i++) {
            System.out.println(all[i]);
        }
    }
}
class Student{
    private String name;
    private int age;
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Student() {
        super();
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
    
}

 

posted @ 2020-09-18 10:49  窦云鹏  阅读(153)  评论(0编辑  收藏  举报