ホームページ  >  記事  >  Java  >  Javaのsplit()関数

Javaのsplit()関数

PHPz
PHPzオリジナル
2024-08-30 15:35:26486ブラウズ

Javaのsplit()関数は、正規表現または指定された区切り文字に基づいて文字列を文字列配列に分割するために使用されます。結果として得られるオブジェクトは、分割された文字列を含む配列です。結果として返される配列では、要素数の制限を渡すことができます。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

例を見てみましょう。

  • 文字列: Java@SplitFunctions
  • 正規表現: @
  • 結果: 「Java」、「SplitFunctions」

上記の例では、必要な正規表現の一致に基づいて文字列を分割します。

構文:

分割関数の構文は次のとおりです。

public String[] split(String regex)

上記の署名では、regex が正規表現を区切っています。文字列の分割に使用する文字を指定します。最後に、結果の戻り値は、正規表現の一致に基づいて文字列を分割する文字列の配列を返します。

Java では、split() 関数はどのように機能しますか?

Java Split() 関数では、さまざまなメソッドを使用して文字列を分割します。 string クラスは、文字列を分割する 2 つのメソッドを作成します。

次のように利用可能な署名を見てみましょう。

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

1. public String[] Split(String regex)

このメソッドは、指定された文字列の正規表現を使用して文字列を分割します。文字列全体が文字列を分割し、その結果として配列文字列として返されます。 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

limit パラメーターはオプションです。分割数を示す整数、および分割制限を超えた項目は配列に含まれません。上記の最初のメソッドは、制限を 0 として渡すことで 2 番目のメソッドを使用します。

コード:

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()関数

ここでは、この関数の 2 番目の引数として制限を与える 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() 関数に入力パラメータを与える必要があります。制限パラメータ

を指​​定した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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:Java newInstance()次の記事:Java newInstance()