In Java, a Zeichenfolge ist ein Objekt das die Zeichenfolge darstellt. Um Strings verwenden zu können, müssen wir die importieren Zeichenfolge Klasse definiert in java.lang Paket. Der String-Array ist ein Array aus Strings mit fester Länge. In diesem Abschnitt werden wir lernen So konvertieren Sie einen String in ein String-Array .
Hinweis: Java-Strings sind unveränderliche Objekte, d. h. der String-Wert kann nach seiner Erstellung nicht mehr geändert werden.
Konvertieren eines Strings in ein String-Array
Es gibt vier Möglichkeiten, einen String in ein String-Array in Java umzuwandeln:
Was ist Hashset Java?
- Verwenden der String.split()-Methode
- Verwenden der Methode Pattern.split()
- Verwendung des String[]-Ansatzes
- Verwendung der toArray()-Methode
Verwenden der String.split()-Methode
Der String.split() Die Methode wird verwendet, um die Zeichenfolge basierend auf dem angegebenen Trennzeichen (Leerzeichen oder andere Symbole) in einzelne Zeichenfolgeneinheiten aufzuteilen. Wir können diese Entitäten direkt in einem String-Array speichern.
Betrachten wir das folgende Beispiel, in dem wir die Methode String.split() verwenden, um einen String in ein String-Array zu konvertieren.
TestSplitMethod.java
public class TestSplitMethod { public static void main(String[] args) { //declaring and initializing a string String str = 'Converting string to string array using split() method'; //declaring an empty string array String[] strArray = null; //converting using String.split() method with whitespace as a delimiter strArray = str.split(' '); //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 = 'Hello,have,a,nice,day'; String[] strArray = null; //empty string array //converting using String.split() method with comma as a delimiter strArray = commaSeparatedStr.split(','); //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'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 = 'Converting string to string array using Pattern.split()'; //declaring an empty string array String[] strArray = null; //parsing white space as a parameter Pattern ptr = Pattern.compile(' '); //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 = 'Hello #a1Ben #a1how #a1are #a1you ?'; //declaring an empty string array String[] strArray = null; //splitting the string with delimiter as #a1 String patternStr = '#a1'; 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 = 'Converting string to string array using String[]'; //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('Hello'); list.add('Welcome'); list.add('To'); list.add('Tutorial'); //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;>
Betrachten wir das folgende Beispiel, in dem wir eine Zeichenfolge in ein Array aufteilen, indem wir das Trennzeichen als Leerzeichen verwenden.
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 = 'Converting string to string array using Pattern.split()'; //declaring an empty string array String[] strArray = null; //parsing white space as a parameter Pattern ptr = Pattern.compile(' '); //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 = 'Hello #a1Ben #a1how #a1are #a1you ?'; //declaring an empty string array String[] strArray = null; //splitting the string with delimiter as #a1 String patternStr = '#a1'; 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 = 'Converting string to string array using String[]'; //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('Hello'); list.add('Welcome'); list.add('To'); list.add('Tutorial'); //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;>
Ausgabe:
Verwendung der toArray()-Methode
Wir können String auch mithilfe von in ein String-Array konvertieren toArray() Methode der Aufführen Klasse. Es verwendet eine Liste vom Typ String als Eingabe und konvertiert jede Entität in ein String-Array.
Betrachten Sie das folgende Beispiel, in dem wir die Liste der Strings in ein String-Array konvertiert haben.
sagte Madhuri
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('Hello'); list.add('Welcome'); list.add('To'); list.add('Tutorial'); //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;>