java中String的常用方法

 

常用方法一:

import org.junit.Test;

public class StringMethodTest {
    
    @Test
    public void test1() {
        String s1 = "HelloWorld";
        
        System.out.println(s1.length());// 返回长度
        System.out.println(s1.charAt(0));// 返回索引字符
        System.out.println(s1.isEmpty());// 判断是否为空
        String s2 = s1.toLowerCase();// 转小写
        String s3 = s1.toUpperCase();// 转大写
        System.out.println(s2 + "," + s3);
        String s4 = "   He  llo   World    ";
        String s5 = s4.trim();// 去首尾空格
        System.out.println("----" + s4 +"----");
        System.out.println("----" + s5 +"----");
        System.out.println(s1.equals(s5));// 比较内容是否相等
        System.out.println(s1.equalsIgnoreCase(s5));// 忽略大小写比较内容是否相等
        String s6 = "abc";
        String s7 = s6.concat("def");// 拼接字符 等价于用"+"
        System.out.println(s7);
        String s8 = "a";
        String s9 = "b";
        System.out.println(s8.compareTo(s9));// a=97 b=98(涉及字符串排序)
        String s10 = "腹有诗书气自华";
        String s11 = s10.substring(2);// 索引2及后面的字符
        System.out.println(s11);
        String s12 = s10.substring(2, 4);// 索引2及索引4前面的字符([a,) 左闭右开)
        System.out.println(s12);
    }
}

常用方法二:

import org.junit.Test;

public class StringMethodTest {
    
    @Test
    public void test2() {
        String str = "helloworld";
        boolean b1 = str.endsWith("rld");// 判断是否"rld"结尾
        System.out.println(b1);
        boolean b2 = str.startsWith("he");// 判断是否"he"开头
        System.out.println(b2);
        boolean b3 = str.startsWith("ll", 2);// 判断索引2是否"ll"开头
        System.out.println(b3);
        System.out.println(str.contains("wo"));// 判断s1是否包含"wo"
        System.out.println(str.indexOf("lo"));// 第一次出现的索引位置
        System.out.println(str.indexOf("rl", 5));// 从索引5开始查询"rl"第一次出现的索引位置
        System.out.println(str.lastIndexOf("or"));// "or"最右边出现处的索引
        System.out.println(str.lastIndexOf("el", 6));// 从索引6开始反向查询"el"第一次出现的索引位置
        // 注:indexOf和lastIndexOf方法如果未找到都是返回-1
    }
}

常用方法三:

import org.junit.Test;

public class StringMethodTest {
    
    @Test
    public void test3() {
        // 替换
        String s1 = "北京最新防疫消息";
        String s2 = s1.replace('北', '东');// 将所有'北'替换成'东'
        System.out.println(s2);
        
        String s3 = s1.replace("北京", "上海");// 将所有"北京"替换成"上海"
        System.out.println(s3);
        
        String s4 = "12hello34world5java7891mysql1456";
        // 把字符串中的数字替换,,如果结果中开头和结尾有,的话去掉
        String s5 = s4.replaceAll("\\d+", ",").replaceAll("^,|,$", "");
        System.out.println(s5);
        
        // 把第一个字符串中的数字替换,
        String s6 = s4.replaceFirst("\\d+", ",");
        System.out.println(s6);
        
        // 匹配
        System.out.println("************");
        String s7 = "12345";
        // 判断s7字符串中全部由数字组成,即有1-n数字组成
        boolean matches = s7.matches("\\d+");
        System.out.println(matches);
        String tel = "0571-4534289";
        // 判断这是否是一个杭州的固定电话
        boolean result = tel.matches("0571-\\d{7,8}");
        System.out.println(result);
        
        // 切片
        System.out.println("************");
        String s8 = "hello|world|java";
        String[] s9 = s8.split("\\|");
        for (int i = 0; i < s9.length; i++) {
            System.out.println(s9[i]);
        }
        System.out.println("");
        s8 = "hello.world.java";
        s9 = s8.split("\\.");
        for (int i = 0; i < s9.length; i++) {
            System.out.println(s9[i]);
        }
    }
}

 

posted @ 2022-09-18 10:32  lai_xinghai  阅读(126)  评论(0)    收藏  举报