首頁  >  文章  >  Java  >  Java 中的 split() 函數

Java 中的 split() 函數

PHPz
PHPz原創
2024-08-30 15:35:26486瀏覽

Java split() 函數用於根據正規表示式或給定的分隔符號將字串拆分為字串數組。結果物件是一個包含分割字串的陣列。在傳回的結果陣列中,我們可以傳遞元素數量的限制。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

讓我們來看看例子,

  • 字串: Java@SplitFunctions
  • 正規表示式: @
  • 結果:「Java」、「SplitFunctions」

上面的範例根據所需正規表示式的符合來分割字串。

文法:

split函數的語法如下,

public String[] split(String regex)

在上面的簽章中,regex 是正規表示式的定界。它指定用於分割字串的字元。最後,結果傳回值傳回字串數組,該數組根據正規表示式的匹配拆分字串。

Java 中 split() 函數如何運作?

在Java Split() 函數中,我們使用各種方法分割字串;string 類別提供了兩種方法來分割字串。

讓我們看看可用的簽名如下,

  • public String[] split(String regex, int limit)
  • public String[] split(String regex)

1.公有字串[] split(字串正規表示式)

此方法使用給定字串上的正規表示式分割字串;整個字串分割字串,結果傳回形式為數組字串。在Java 1.4中,引入了上述方法。讓我們看看分割字串的範例以及對應的結果輸出,

代碼:

String string_1="JavaProgram";
System.out.println(Arrays.toString(s.split("a")));

輸出:

Java 中的 split() 函數

2. public String[] split(String regex, int limit)

當我們需要將Java字串拆分為有限數量的字串時,我們使用此方法;為此,我們採用這種方法;讓我們看一下字串的範例,其中包含包含名稱和地址的字串變量,分隔符號為逗號,以下地址包含逗號,因此我們採用此方法。

代碼:

String s = "Spencer Plaza, New York, USA";
String[] data = s.split(",", 2);
System.out.println("Name = "+data[0]); // Spencer Plaza
System.out.println("Address = "+data[1]); //New York,USA

限制參數是可選的。表示分割數量的整數,分割限制之後的項目將不包含在陣列中。上面的第一種方法透過將限制傳遞為 0 來使用第二種方法。

代碼:

public String[] split(String regex) {
return split(regex, 0);
}

split() 函數範例

以下範例如下:

範例#1

代碼:

import java.io.*;
public class Program_1
{
public static void main(String args[]) {
String string_1 = new String("Welcome-to-JavaProgramming");
System.out.println("Output Value :" );
for (String res: string_1.split("-"))
{
System.out.println(res);
}
}
}

輸出:

Java 中的 split() 函數

對需要提供和分割分隔符號參數的字串使用 split() 函數,我們將使用分隔符號作為逗號(,),傳回結果將是陣列分割。每次分割操作後,輸出都會列印稱為陣列元素的每個字串,如下所示,

範例#2

代碼:

import java.io.*;
class Program_2
{
public static void main(String []args)
{
String string_1 = "String, Functions, Split, Methods, Demo";
String[] split_array = string_1.split(", ");
for (int i=0; i < split_array.length; i++)
{
System.out.println(split_array [i]);
}
}
}

輸出:

Java 中的 split() 函數

在這裡,我們傳遞 split,它作為該函數的第二個參數。這限制了分割字串的數量。

範例 #3

代碼:

import java.io.*;
public class Program_3
{
public static void main(String[] args)
{
String string_1 = "JavatSplittFunction";
System.out.println("Result:");
String[] arrSplit = string_1.split("t", 0);
for (String a : arrSplit)
{
System.out.println(a);
}
System.out.println("Length of Split Array: "+ arrSplit.length);
}
}

輸出:

Java 中的 split() 函數

帶有 limit 參數的 Split() 方法

帶有 limit 參數的 Split() 方法會分割有限數量的字串。 split() 和帶有 limit 參數的 split() 之間的差異在於它限制了分割後傳回的字串數量。對於限制,我們需要為 split() 函數提供一個輸入參數。讓我們來看看帶有 limit 參數的 split() 方法的用法,

public String[] split(String regex, int limit)

這裡參數regex對正規表示式進行界定,限制是針對結果閾值。限制有 3 個值,它們是:

  • limit > 0: If the limit is set as >0, the resultant array length must not be more significant than n, which is applied at most limit-1 times. The last entry contains input with the last matched delimiter.
  • limit < 0:If the limit is set as <0, the resultant array with any number of lengths can apply the pattern as many times as possible.
  • limit = 0: If the limit is set as equal to 0, the resulting array has any number of lengths, but the empty string will be discarded, and this limit is functional as many times as possible.
  • This parameter’s return value will be an array of string objects by splitting the given string accords to the limit parameter. The PatternSyntacException will occur if the given regular expression syntax is invalid while executing the code. Let’s see the example program for the split() method on a string with the limit parameter.

    Example #4

    Code:

    public class Program_4
    {
    public static void main(String args[])
    {
    String string_1 = "238-347-9288";
    String[] stringArray = string_1.split("8",2);
    System.out.println("Split() Method with the Limit Parameter");
    System.out.println("\nLimit Value is +ve");
    System.out.println("Sub-Strings: "+stringArray.length);
    for(int i=0; i<stringArray.length; i++)
    {  System.out.println("string_1["+i+"] : "+stringArray[i]);
    }
    String[] stringArray2 = string_1.split("8",-3);
    System.out.println("Limit Value is -ve");
    System.out.println("Sub-Strings: "+stringArray2.length);
    for(int i=0; i<stringArray2.length; i++)
    {
    System.out.println("string_1["+i+"] : "+stringArray2[i]);
    }
    String[] stringArray3 = string_1.split("8",0);
    System.out.println("Limit Value is 0");
    System.out.println("Sub-Strings: "+stringArray3.length);
    for(int i=0; i<stringArray3.length; i++)
    {
    System.out.println("string_1["+i+"] : "+stringArray3[i]);
    }
    }
    }

    Output:

    Java 中的 split() 函數

    The above program shows that the split() method works by the specified limit parameter, as seen by the output:

    1. The number of substrings in the resulting array is two when the limit is 2.
    2. Setting the limit to -3 causes the string to split into 4 substrings, including the trailing spaces.
    3. If you set the limit to 0, it eliminates the trailing spaces. As a result, the input string splits into 2 substrings.

    Conclusion

    At the end of the ‘Split() Function in Java’ article, we learned how to split strings using the split() method in different Java approaches. I hope in this article; you understand everything that has been shared with examples.

    以上是Java 中的 split() 函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java newInstance()下一篇:Java newInstance()