Java 字符串常用方法

Java 字符串 trim() 方法用于去除字符串前后的空白符。空白符包括空格、制表符、全角空格和不间断空格。

Java 字符串 trim() 方法检查字符串前后的这个 Unicode 值,如果存在空白符Unicode则该方法删除空白符并返回去除空白符后的字符串。

字符串 trim() 方法不会去除中间空白符。

语法

Java trim() 方法的语法如下:

public String trim()

    返回值

    去除前后空白符的字符串。

    方法示例1

    文件名: StringTrimExample.java

    public class StringTrimExample{
      public static void main(String args[]){
        String s1="  hello string   ";
        System.out.println(s1+"yxjc123");//没有 trim()
        System.out.println(s1.trim()+"yxjc123");//带trim()
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出

      hello string   yxjc123
    hello stringyxjc123

    方法示例1

    文件名: StringTrimExample2.java

    public class StringTrimExample2 {  
      public static void main(String[] args) {
        String s1 ="  hello java string   ";
        System.out.println(s1.length());
        System.out.println(s1); //不带trim()
        String tr = s1.trim();
        System.out.println(tr.length());
        System.out.println(tr); //带trim()
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出

    22
    hello java string
    17
    hello java string

    方法示例3

    文件名: TrimExample3.java

    public class TrimExample3
    {
      // 主方法
      public static void main(String argvs[])
      {    
        String str = " abc ";    
        if((str.trim()).length() > 0){
          System.out.println("字符串包含空白符以外的字符 \n");
        }
        else {
          System.out.println("字符串只包含空白符 \n");
        }    
        str = "  ";    
        if((str.trim()).length() > 0){
          System.out.println("字符串包含空白符以外的字符 \n");
        }
        else{
          System.out.println("字符串只包含空白符\n");
        }
        
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    输出

    字符串包含空白符以外的字符 

    字符串只包含空白符

    方法示例4

    由于 Java 中的字符串是不可变的;因此,当 trim() 方法通过trim()空白符来操作字符串时,它会返回一个新字符串。

    文件名: TrimExample4.java

    public class TrimExample4
    {
      // 主方法
      public static void main(String argvs[])
      {
        
        // 字符串包含空白符
        // 因此,trim()空白符会导致生成新的字符串      
        String str = " abc ";
        
        // str1 存储一个新字符串
        String str1 = str.trim();
        
        // str和str1的hashcode不同
        System.out.println(str.hashCode());
        System.out.println(str1.hashCode() + "\n");
        
        // 字符串 s 中不存在空白符
        String s = "xyz";
        String s1 = s.trim();
        
        // s和s1的hashcode相同
        System.out.println(s.hashCode());
        System.out.println(s1.hashCode());
        
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    输出

    32539678
    96354

    119193
    119193

    内部实现

    public String trim() {  
      int len = value.length;
      int st = 0;
      char[] val = value;  /* avoid getfield opcode */
      
      while ((st < len) && (val[st] <= ' ')) {
        st++;
      }
      while ((st < len) && (val[len - 1] <= ' ')) {
        len--;
      }
      return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12