String 常用的方法

常用

trim()去除字符串前后空格

    @Test
    public void test(){
        System.out.println("  hello hello hello ".trim());
        System.out.println("  hello hello hello ");
    }
hello hello hello
  hello hello hello 

substring() 截取字符串

    @Test
    public void testSubstring(){
        String str="abcdef";

        System.out.println(str.substring(1));//bcdef
        System.out.println(str.substring(0,2));//ab
        System.out.println(str.substring(1,2));//b
    }

charAt()获取字符串某个位置的字符

    @Test
    public void testCharAt(){
        String s="hello";
        System.out.println(s.charAt(0));//h
    }

转换

toCharArray() 转换为字符数组

    @Test
    public void testToCharArray(){
        String s="hello";
        char[] c=s.toCharArray();
    }
posted @ 2021-08-06 14:46  懒鑫人  阅读(31)  评论(0编辑  收藏  举报