<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # `String.repeat()` – 在 Java 中重復字符串 N 次 > 原文: [https://howtodoinjava.com/java11/repeat-string-n-times/](https://howtodoinjava.com/java11/repeat-string-n-times/) 通過簡單的 Java 程序,學習將給定的字符串重復 N 次,以產生包含所有重復的新字符串。 我們將使用方法`Sting.repeat(N)`(自 [Java 11](https://howtodoinjava.com/java11/features-enhancements/) 起),并使用可用于 [Java 10](https://howtodoinjava.com/java10/java10-features/) 的正則表達式。 ## 1\. `String.repeat()` API(自 Java 11 起) 此方法返回一個字符串,該字符串的值是重復`count`次的給定字符串的連接。 如果字符串為空或`count`為零,則返回空字符串。 #### 1.1 語法 ```java /** * Parameters: * count - number of times to repeat * * Returns: * A string composed of this string repeated count times or the empty string if this string is empty or count is zero * * Throws: * IllegalArgumentException - if the count is negative. */ public String repeat?(int count) ``` #### 1.2 示例 Java 程序將字符串`"Abc"`重復 3 次。 ```java public class Main { public static void main(String[] args) { String str = "Abc"; System.out.println( str.repeat(3) ); } } ``` 程序輸出。 ```java AbcAbcAbc ``` ## 2\. 使用正則表達式重復字符串(直到 Java 10) 如果您正在使用 JDK < = 10,則可以考慮使用正則表達式將字符串重復 N 次。 Java program to repeat string ‘Abc’ to 3 times. ```java public class Main { public static void main(String[] args) { String str = "Abc"; String repeated = new String(new char[3]).replace("\0", str); System.out.println(repeated); } } ``` 程序輸出: ```java AbcAbcAbc ``` ## 3\. Apache Common 的`StringUtils`類 如果不是正則表達式,則可以使用`StringUtils`類及其方法[`repeat(times)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringUtils.html#repeat(java.lang.String,%20int))。 ```java import org.apache.commons.lang3.StringUtils; public class Main { public static void main(String[] args) { String str = "Abc"; String repeated = StringUtils.repeat(str, 3); System.out.println(repeated); } } ``` 程序輸出: ```java AbcAbcAbc ``` 向我提供有關**如何在 Java** 中將字符串重復 N 次的問題。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看