Java String trim()方法

Java String类的trim()方法用于消除字符串中的前导和尾部空格。空格字符的Unicode值为'u0020'。在Java字符串的trim()方法中,它会检查字符串前后的Unicode值是否存在,如果存在,则将空格删除并返回修剪后的字符串。

语法

String类trim()方法的语法如下:

public String trim()  

返回值

去除前导和尾部空格的字符串

内部实现

public String trim() {    
        int len = value.length;    
        int st = 0;    
        char[] val = value;    /* 避免 getfield 操作码 */    
    
        while ((st < len) && (val[st] <= ' ')) {    
            st++;    
        }    
        while ((st < len) && (val[len - 1] <= ' ')) {    
            len--;    
        }    
        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;    
    }    

Java String trim()方法示例

文件名:StringTrimExample.java

public class StringTrimExample{  
public static void main(String args[]){  
String s1="  hello string   ";  
System.out.println(s1+"javatpoint");//没有trim()  
System.out.println(s1.trim()+"javatpoint");//有trim()  
}}  

输出:

hello string   javatpoint
hello stringjavatpoint   

Java String trim()方法示例2

该示例演示了trim()方法的使用。该方法会删除所有尾部空格,因此字符串的长度也会减少。让我们看一个例子。

文件名: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); //Without trim()    
        String tr = s1.trim();    
        System.out.println(tr.length());    
        System.out.println(tr); //With trim()    
    }    
}    

输出:

22
  hello java string   
17
hello java string

Java String trim()方法示例3

trim()方法可用于检查字符串是否只包含空格。下面的示例演示了相同的情况。

文件名:TrimExample3.java

public class TrimExample3  
{  
// main method  
public static void main(String argvs[])  
{  
  
String str = " abc ";  
  
if((str.trim()).length() > 0)  
{  
System.out.println("The string contains characters other than white spaces \n");  
}  
else  
{  
System.out.println("The string contains only white spaces \n");     
}  
  
str = "    ";  
  
if((str.trim()).length() > 0)  
{  
System.out.println("The string contains characters other than white spaces \n");  
}  
else  
{  
System.out.println("The string contains only white spaces \n");     
}  
  
}  
}  

输出:

The string contains characters other than white spaces 

The string contains only white spaces

Java String trim()方法示例4

由于Java中的字符串是不可变的,因此当trim()方法通过修剪空格来操作字符串时,它会返回一个新的字符串。如果trim()方法没有进行操作,则返回的是相同字符串的引用。请观察以下示例。

文件名:TrimExample4.java

public class TrimExample4  
{  
// 主要方法 
public static void main(String argvs[])  
{  
  
// 字符串包含空格  
// 因此,修剪空格会导致  
// 生成新字符串
String str = " abc ";  
  
//str1 存储一个新的字符串  
String str1 = str.trim();  
  
//str 和 str1 的哈希码不同 
System.out.println(str.hashCode());  
System.out.println(str1.hashCode() + "\n");  
  
// 字符串中没有空格 
// 因此,返回 s 的引用  
// 当调用 trim() 方法时
String s = "xyz";  
String s1 = s.trim();  
  
// s 和 s1 的哈希码相同
System.out.println(s.hashCode());  
System.out.println(s1.hashCode());  
  
}  
}  

输出:

The string contains characters other than white spaces 

The string contains only white spaces

标签: java, Java面试题, Java下载, java教程, java技术, Java学习, Java学习教程, Java语言, Java开发, Java入门教程, Java进阶教程, Java高级教程, Java笔试题, Java编程思想