<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java `String split()`方法 > 原文: [https://beginnersbook.com/2013/12/java-string-split-method-example/](https://beginnersbook.com/2013/12/java-string-split-method-example/) Java [`String`](https://beginnersbook.com/2013/12/java-strings/)`split`方法用于根據給定的分隔符或[正則表達式](https://beginnersbook.com/2014/08/java-regex-tutorial/)將`String`拆分為其子串。 例如: ```java String: [email?protected] Regular Expression: @ Output : {"chaitanya", "singh"} ``` ## Java 字符串拆分方法 我們在`String`類中有兩種`split()`方法。 1. `String[] split(String regex)`:在根據分隔正則表達式拆分輸入`String`后返回一個字符串數組。 2. `String[] split(String regex, int limit)`:當我們想要限制子串時,使用這個`String split`方法。此方法與上述方法的唯一區別在于它限制了拆分后返回的字符串數。對于例如`split("anydelimiter", 3)`將返回僅 3 個字符串的數組,即使字符串中的分隔符超過 3 次也是如此。 如果**限制為負**,則返回的數組將具有盡可能多的子串,但是當**限制為零**時,返回的數組將具有除尾隨空字符串之外的所有子串。 如果指定正則表達式的語法無效,則拋出 [`PatternSyntaxException`](https://docs.oracle.com/javase/7/docs/api/java/util/regex/PatternSyntaxException.html) 。 ## Java `String`拆分示例 ```java public class SplitExample{ public static void main(String args[]){ // This is out input String String str = new String("28/12/2013"); System.out.println("split(String regex):"); /* Here we are using first variation of java string split method * which splits the string into substring based on the regular * expression, there is no limit on the substrings */ String array1[]= str.split("/"); for (String temp: array1){ System.out.println(temp); } /* Using second variation of split method here. Since the limit is passed * as 2\. This method would only produce two substrings. */ System.out.println("split(String regex, int limit) with limit=2:"); String array2[]= str.split("/", 2); for (String temp: array2){ System.out.println(temp); } System.out.println("split(String regex, int limit) with limit=0:"); String array3[]= str.split("/", 0); for (String temp: array3){ System.out.println(temp); } /* When we pass limit as negative. The split method works same as the first variation * because negative limit says that the method returns substrings with no limit. */ System.out.println("split(String regex, int limit) with limit=-5:"); String array4[]= str.split("/", -5); for (String temp: array4){ System.out.println(temp); } } } ``` **輸出:** ```java split(String regex): 28 12 2013 split(String regex, int limit) with limit=2: 28 12/2013 split(String regex, int limit) with limit=0: 28 12 2013 split(String regex, int limit) with limit=-5: 28 12 2013 ``` ## java 字符串拆分方法中零和負限制之間的差異 在上面的示例中,`split("/", 0)`和`split("/", -5)`返回相同的值,但在某些情況下,結果會有所不同。讓我們通過一個例子來看看這兩者之間的區別: ```java String s="bbaaccaa"; String arr1[]= s.split("a", -1); String arr2[]= s.split("a", 0); ``` 在這種情況下,`arr1`將具有`{"bb","","cc","",""}`但是`arr2`將具有`{"bb","","cc"}`,因為限制零不包括試驗空字符串。 讓我們看看完整的程序。 ```java public class JavaExample{ public static void main(String args[]){ // This is out input String String s = new String("bbaaccaa"); //Splitting with limit as 0 String arr2[]= s.split("a", 0); System.out.println("Zero Limit split:"); for (String str2: arr2){ System.out.println(str2); } //Splitting with negative limit String arr1[]= s.split("a", -1); System.out.println("Negative Limit split:"); for (String str: arr1){ System.out.println(str); } System.out.println("End of program"); } } ``` **輸出:** ![Java String split](https://img.kancloud.cn/71/22/712225767f44ed375089578c02f3e1ac_700x1024.jpg) ## Java 帶有多個分隔符(特殊字符)的字符串拆分 讓我們看看我們如何在使用`split()`方法時傳遞多個分隔符。在這個例子中,我們基于多個特殊字符拆分輸入字符串。 ```java public class JavaExample{ public static void main(String args[]){ String s = " ,ab;gh,bc;pq#kk$bb"; String[] str = s.split("[,;#$]"); //Total how many substrings? The array length System.out.println("Number of substrings: "+str.length); for (int i=0; i < str.length; i++) { System.out.println("Str["+i+"]:"+str[i]); } } } ``` **輸出:** ![Java string split multiple characters, split multiple special characters](https://img.kancloud.cn/1e/3d/1e3dfeeee289b026da3eb12f3f2cd41f_1004x848.jpg) 讓我們再練習幾個例子: **示例:Java `String split`方法中的正則表達式** ```java public class SplitExample1 { public static void main(String args[]) { String str = "helloxyzhixyzbye"; String[] arr = str.split("xyz"); for (String s : arr) System.out.println(s); } } ``` **輸出:** ```java hello hi bye ``` **示例:基于空格分割字符串** ```java public class SplitExample2 { public static void main(String args[]) { String str = "My name is Chaitanya"; //regular expression is a whitespace here String[] arr = str.split(" "); for (String s : arr) System.out.println(s); } } ``` **輸出:** ```java My name is Chaitanya ```
                  <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>

                              哎呀哎呀视频在线观看