logo

Javaで文字列を文字列配列に変換するにはどうすればよいですか?

Java では、 です 物体 文字の並びを表します。文字列を使用するには、 で定義されたクラス java.lang パッケージ。の 文字列配列 です 配列 固定長の文字列。このセクションでは、次のことを学びます 文字列を文字列配列に変換する方法

注: Java 文字列は不変オブジェクトです。つまり、文字列値は作成後に変更できません。

文字列を文字列配列に変換する

Java で String を String 配列に変換するには、次の 4 つの方法があります。

  1. String.split() メソッドの使用
  2. Pattern.split() メソッドの使用
  3. String[ ] アプローチの使用
  4. toArray() メソッドの使用

String.split() メソッドの使用

String.split() このメソッドは、指定された区切り文字 (空白またはその他の記号) に基づいて文字列を個々の文字列エンティティに分割するために使用されます。これらのエンティティを文字列配列に直接保存できます。

String.split() メソッドを使用して文字列を文字列配列に変換する次の例を考えてみましょう。

jdbc

TestSplitMethod.java

 public class TestSplitMethod { public static void main(String[] args) { //declaring and initializing a string String str = &apos;Converting string to string array using split() method&apos;; //declaring an empty string array String[] strArray = null; //converting using String.split() method with whitespace as a delimiter strArray = str.split(&apos; &apos;); //printing the converted string array for (int i = 0; i<strarray.length; i++){ system.out.println(strarray[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java.webp" alt="How to convert String to String array in Java"> <p> <strong>Example 2:</strong> </p> <p>In the following example, we have converted the string to a string array based on the delimiter that is a <strong>,</strong> (comma).</p> <p> <strong>TestSplitMethod2.java</strong> </p> <pre> public class TestSplitMethod2 { public static void main(String[] args) { String commaSeparatedStr = &apos;Hello,have,a,nice,day&apos;; String[] strArray = null; //empty string array //converting using String.split() method with comma as a delimiter strArray = commaSeparatedStr.split(&apos;,&apos;); //printing the converted string array for (int i = 0; i<strarray.length; i++){ system.out.println(strarray[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-2.webp" alt="How to convert String to String array in Java"> <h3>Using Pattern.split() Method</h3> <p>The Pattern.split() method is used to split the string into an array of strings with the help of regular expression (pattern) as the delimiter.</p> <p>In order to use the method, we need to import the <strong>Pattern</strong> class in our Java code as:</p> <pre> import java.util.regex.Pattern; </pre> <p>Let&apos;s consider the following example where we split a string into an array by using the delimiter as whitespace.</p> <p> <strong>SplitMethodOfPatternClass.java</strong> </p> <pre> //importing Pattern class import java.util.regex.Pattern; public class SplitMethodOfPatternClass { public static void main(String[] args) { //declaring and initializing a string String str = &apos;Converting string to string array using Pattern.split()&apos;; //declaring an empty string array String[] strArray = null; //parsing white space as a parameter Pattern ptr = Pattern.compile(&apos; &apos;); //storing the string elements in array after splitting strArray = ptr.split(str); //printing the converted string array for (int i = 0; i<strarray.length; i++){ system.out.println(strarray[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-3.webp" alt="How to convert String to String array in Java"> <p> <strong>Example 2:</strong> </p> <p>We can also split a string into an array by using any string or pattern as a delimiter. Here, we have used the delimiter <strong>#a1.</strong> </p> <p> <strong>SplitMethodOfPatternClass2.java</strong> </p> <pre> //importing Pattern class import java.util.regex.Pattern; public class SplitMethodOfPatternClass2 { public static void main(String[] args) { //declaring and initializing a string with a separator String str = &apos;Hello #a1Ben #a1how #a1are #a1you ?&apos;; //declaring an empty string array String[] strArray = null; //splitting the string with delimiter as #a1 String patternStr = &apos;#a1&apos;; Pattern ptr = Pattern.compile(patternStr); //storing the string elements in array after splitting strArray = ptr.split(str); //printing the converted string array for (int i = 0; i<strarray.length; i++){ system.out.println(strarray[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-4.webp" alt="How to convert String to String array in Java"> <p>The above example splits the single string into five separate strings based on the delimiter #a1. The parts of the string that matched the delimiter are not included in the array.</p> <h3>Using String[ ] Approach</h3> <p>We can simply convert string to string array by using the string index [ ]. Here, we pass the string to <strong>String [ ] {}.</strong> </p> <p>Consider the following example where we have used the String[] to convert a string into a string array.</p> <p> <strong>StrToStrArray.java</strong> </p> <pre> import java.util.Arrays; public class StrToStrArray { public static void main(String[] args) { //declaring and initializing a string String str = &apos;Converting string to string array using String[]&apos;; //passing the string to String[] {} String[] strArray = new String[] {str}; //printing the string array using Arrays.toString() System.out.println(Arrays.toString(strArray)); } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-5.webp" alt="How to convert String to String array in Java"> <h3>Using toArray() Method</h3> <p>We can also convert String to String array by using the <a href="/java-list-toarray-method">toArray()</a> method of the <a href="/java-list">List</a> class. It takes a list of type String as the input and converts each entity into a string array.</p> <p>Consider the following example where we have converted the list of strings into a string array.</p> <p> <strong>StringListtoArray.java</strong> </p> <pre> //importing ArrayList and List class import java.util.ArrayList; import java.util.List; public class StringListtoArray { public static void main(String[] args) { //creating a list of type string List list = new ArrayList (); //adding elements to list list.add(&apos;Hello&apos;); list.add(&apos;Welcome&apos;); list.add(&apos;To&apos;); list.add(&apos;Tutorial&apos;); //size of list int list_size = list.size(); //creating string array String[] strArray = new String[list_size]; //converting to string array list.toArray(strArray); //printing the string array for(int i = 0; i <strarray.length; i++) { system.out.println(strarray[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-6.webp" alt="How to convert String to String array in Java"> <hr></strarray.length;></pre></strarray.length;></pre></strarray.length;></pre></strarray.length;></pre></strarray.length;>

区切り文字を空白として使用して文字列を配列に分割する次の例を考えてみましょう。

SplitMethodOfPatternClass.java

 //importing Pattern class import java.util.regex.Pattern; public class SplitMethodOfPatternClass { public static void main(String[] args) { //declaring and initializing a string String str = &apos;Converting string to string array using Pattern.split()&apos;; //declaring an empty string array String[] strArray = null; //parsing white space as a parameter Pattern ptr = Pattern.compile(&apos; &apos;); //storing the string elements in array after splitting strArray = ptr.split(str); //printing the converted string array for (int i = 0; i<strarray.length; i++){ system.out.println(strarray[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-3.webp" alt="How to convert String to String array in Java"> <p> <strong>Example 2:</strong> </p> <p>We can also split a string into an array by using any string or pattern as a delimiter. Here, we have used the delimiter <strong>#a1.</strong> </p> <p> <strong>SplitMethodOfPatternClass2.java</strong> </p> <pre> //importing Pattern class import java.util.regex.Pattern; public class SplitMethodOfPatternClass2 { public static void main(String[] args) { //declaring and initializing a string with a separator String str = &apos;Hello #a1Ben #a1how #a1are #a1you ?&apos;; //declaring an empty string array String[] strArray = null; //splitting the string with delimiter as #a1 String patternStr = &apos;#a1&apos;; Pattern ptr = Pattern.compile(patternStr); //storing the string elements in array after splitting strArray = ptr.split(str); //printing the converted string array for (int i = 0; i<strarray.length; i++){ system.out.println(strarray[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-4.webp" alt="How to convert String to String array in Java"> <p>The above example splits the single string into five separate strings based on the delimiter #a1. The parts of the string that matched the delimiter are not included in the array.</p> <h3>Using String[ ] Approach</h3> <p>We can simply convert string to string array by using the string index [ ]. Here, we pass the string to <strong>String [ ] {}.</strong> </p> <p>Consider the following example where we have used the String[] to convert a string into a string array.</p> <p> <strong>StrToStrArray.java</strong> </p> <pre> import java.util.Arrays; public class StrToStrArray { public static void main(String[] args) { //declaring and initializing a string String str = &apos;Converting string to string array using String[]&apos;; //passing the string to String[] {} String[] strArray = new String[] {str}; //printing the string array using Arrays.toString() System.out.println(Arrays.toString(strArray)); } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-5.webp" alt="How to convert String to String array in Java"> <h3>Using toArray() Method</h3> <p>We can also convert String to String array by using the <a href="/java-list-toarray-method">toArray()</a> method of the <a href="/java-list">List</a> class. It takes a list of type String as the input and converts each entity into a string array.</p> <p>Consider the following example where we have converted the list of strings into a string array.</p> <p> <strong>StringListtoArray.java</strong> </p> <pre> //importing ArrayList and List class import java.util.ArrayList; import java.util.List; public class StringListtoArray { public static void main(String[] args) { //creating a list of type string List list = new ArrayList (); //adding elements to list list.add(&apos;Hello&apos;); list.add(&apos;Welcome&apos;); list.add(&apos;To&apos;); list.add(&apos;Tutorial&apos;); //size of list int list_size = list.size(); //creating string array String[] strArray = new String[list_size]; //converting to string array list.toArray(strArray); //printing the string array for(int i = 0; i <strarray.length; i++) { system.out.println(strarray[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-6.webp" alt="How to convert String to String array in Java"> <hr></strarray.length;></pre></strarray.length;></pre></strarray.length;>

出力:

Javaで文字列を文字列配列に変換する方法

toArray() メソッドの使用

を使用して String を String 配列に変換することもできます。 toArray() の方法 リスト クラス。 String 型のリストを入力として受け取り、各エンティティを文字列配列に変換します。

文字列のリストを文字列配列に変換した次の例を考えてみましょう。

StringListtoArray.java

 //importing ArrayList and List class import java.util.ArrayList; import java.util.List; public class StringListtoArray { public static void main(String[] args) { //creating a list of type string List list = new ArrayList (); //adding elements to list list.add(&apos;Hello&apos;); list.add(&apos;Welcome&apos;); list.add(&apos;To&apos;); list.add(&apos;Tutorial&apos;); //size of list int list_size = list.size(); //creating string array String[] strArray = new String[list_size]; //converting to string array list.toArray(strArray); //printing the string array for(int i = 0; i <strarray.length; i++) { system.out.println(strarray[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/38/how-convert-string-string-array-java-6.webp" alt="How to convert String to String array in Java"> <hr></strarray.length;>