<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # `String.strip()` – 刪除開頭和結尾的空格 > 原文: [https://howtodoinjava.com/java11/strip-remove-white-spaces/](https://howtodoinjava.com/java11/strip-remove-white-spaces/) 在 [Java 11](https://howtodoinjava.com/java11/features-enhancements/) 中,學習如何使用`String`類的`strip()`,`stripLeading()`和`stripTrailing()`方法從給定的字符串中刪除不需要的空格。 ## 1\. `String.strip()`API 從 Java 11 開始,`String`類包含 3 個以上的方法,這些方法有助于消除多余的空白。 這些方法使用[`Character.isWhitespace(char)`](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Character.html#isWhitespace(int)) 方法確定空白字符。 * **`String.strip()`** – 返回其值為字符串的字符串,其中**刪除了所有前導和尾隨空白**。 請注意,`String.trim()`方法也會產生相同的結果。 * **`String.stripLeading()`** – 返回其值為字符串的字符串,其中**刪除了所有前導空白**。 * **`String.stripTrailing()`** – 返回其值為字符串的字符串,其中**刪除了所有尾隨空白**。 ```java public class Main { public static void main(String[] args) { String str = " Hello World !! "; System.out.println( str.strip() ); //"Hello World !!" System.out.println( str.stripLeading() ); //"Hello World !! " System.out.println( str.stripTrailing() ); //" Hello World !!" } } ``` ## 2\. 使用正則表達式修剪空白(包括制表符) 在不使用 Java 11 的情況下,可以使用正則表達式來修剪字符串周圍的空白。 | 正則表達式 | 描述 | | --- | --- | | <code>^[\t]+&#124;[\t]+$</code> | 刪除開頭和結尾的空格 | | **`^[\t]+`** | 僅刪除開頭的空格 | | **`[\t]+$`** | 僅刪除結尾的空格 | ```java public class Main { public static void main(String[] args) { String str = " Hello World !! "; System.out.println( str.replaceAll("^[ \t]+|[ \t]+$", "") ); //"Hello World !!" System.out.println( str.replaceAll("^[ \t]+", "") ); //"Hello World !! " System.out.println( str.replaceAll("[ \t]+$", "") ); //" Hello World !!" } } ``` 向我提供有關**如何在 Java 中從字符串**中刪除空格和制表符的問題。 學習愉快! 參考: [`regular-expressions.info`](https://www.regular-expressions.info/examples.html) [Java `String.strip()` API 文檔](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/String.html#strip())
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看