Java - String trim() 方法



描述

Java String trim() 方法用於移除字串開頭和結尾的空格。它返回一個字串副本,其中開頭和結尾的空格已被省略;如果沒有開頭或結尾的空格,則此方法返回當前字串。

此方法不會更改 String 物件的值。我們只需要將新的 String 物件賦值給一個新變數或重新賦值給原始 String,才能訪問它。

注意 - 請記住,trim() 方法不會消除中間的空格。

語法

以下是Java String trim() 方法的語法:

public String trim()

引數

此方法不接受任何引數。

返回值

此方法返回此字串的副本,其中已移除開頭和結尾的空格;如果此字串沒有開頭或結尾的空格,則返回此字串本身。

從字串中去除開頭和結尾空格的示例

以下示例演示瞭如何使用 Java String trim() 方法從給定的字串 " This is TutorialsPoint " 中移除開頭和結尾的空格:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      
      // string with leading and trailing white space
      String str = " This is TutorialsPoint ";    
      System.out.print("Before trim = ");
      System.out.println(".." + str + "..");    
      
      // leading and trailing white space removed
      System.out.print("After trim = ");
      System.out.println(".." + str.trim() + "..");
   }
}

輸出

如果您編譯並執行上面的程式,則輸出將顯示如下:

Before trim = .. This is TutorialsPoint ..
After trim = ..This is TutorialsPoint..

驗證從字串中去除開頭和結尾空格後的長度示例

下面是另一個示例,它使用 Java 中的 trim() 方法移除所有開頭和結尾的空格。在這裡,我們驗證了移除空格後的字串長度:

package com.tutorialspoint;

import java.util.Locale;

public class StringDemo {
   public static void main(String[] args) {
      String str = "    Tutorials Point      ";
      System.out.println("The length of the string before trimming is: " + str.length());
      System.out.println("The string without trimming is: " + str);
      String t = str.trim();
      System.out.println("The length of the string after trimming is: " + t.length());
      System.out.println("The string with trimming is:" + t);
   }
}

輸出

如果您編譯並執行上面的程式,它將產生以下結果:

The length of the string before trimming is: 25
The string without trimming is:     Tutorials Point
The length of the string after trimming is: 15
The string with trimming is:Tutorials Point

檢查從字串中去除開頭和結尾空格後的空格示例

讓我們建立另一個程式碼,使用 if-else 條件來檢查給定的字串是否只包含空格:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String argvs[]) {
      String s = "  Tutorials Point  ";
      if ((s.trim()).length() > 0) {
         System.out.println("The string consists of characters other than the white spaces \n");
      } else {
         System.out.println("The string consists of only white spaces \n");
      }
      s = "      ";
      if ((s.trim()).length() > 0) {
         System.out.println("The string consists of characters other than the white spaces \n");
      } else {
         System.out.println("The string consists of only white spaces \n");
      }
   }
}

輸出

執行上面的程式後,獲得的輸出如下:

The string consists of characters other than the white spaces

The string consists of only white spaces

檢查從字串中去除開頭和結尾空格後的雜湊碼示例

由於 Java 中的字串是不可變的,因此使用 trim() 方法從字串中移除空格時,會返回一個新字串。如果 trim() 方法沒有進行操作,則會返回對同一字串的引用,如下例所示:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String argvs[]) {
      String s = "  Tutorials Point  ";
      String s1 = s.trim();
      
      // the hashcode of s and s1 is different
      System.out.println("The hashcode of string s is: " + s.hashCode());
      System.out.println("The hashcode of string s1 is: " + s1.hashCode());
      String x = "calm";
      String y = x.trim();
      
      // the hashcode of x and y is the same
      System.out.println("The hashcode of string x is: " + x.hashCode());
      System.out.println("The hashcode of string y is: " + y.hashCode());
   }
}

輸出

上述程式碼的輸出如下所示:

The hashcode of string s is: 932888837
The hashcode of string s1 is: -1622622651
The hashcode of string x is: 3045983
The hashcode of string y is: 3045983
java_lang_string.htm
廣告
© . All rights reserved.