logo

Hvordan konverteres String til String array i Java?

I Java, en Snor er en objekt der repræsenterer rækkefølgen af ​​tegn. For at bruge Strings skal vi importere Snor klasse defineret i java.lang pakke. Det String array er en array af strenge med en fast længde. I dette afsnit vil vi lære hvordan man konverterer String til String array .

Bemærk: Java-strenge er uforanderlige objekter, dvs. strengværdien kan ikke ændres, når den først er oprettet.

Konvertering af streng til streng array

Der er fire måder at konvertere en streng til String-array i Java:

lighed af strenge i java
  1. Bruger String.split() metoden
  2. Bruger Pattern.split()-metoden
  3. Brug af String[ ]-tilgang
  4. Brug af toArray()-metoden

Bruger String.split() metoden

Det String.split() metoden bruges til at opdele strengen i individuelle strengenheder baseret på den givne afgrænsning (whitespace eller andre symboler). Vi kan gemme disse entiteter direkte i et string-array.

Lad os overveje følgende eksempel, hvor vi bruger metoden String.split() til at konvertere en streng til en strengmatrix.

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;>

Lad os overveje følgende eksempel, hvor vi opdeler en streng i et array ved at bruge afgrænsningstegnet som mellemrum.

SplitMethodOfPatternClass.java

navn på makeupprodukter
 //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;>

Produktion:

Sådan konverteres String til String array i Java

Brug af toArray()-metoden

Vi kan også konvertere String til String array ved at bruge toArray() metoden til Liste klasse. Det tager en liste af typen String som input og konverterer hver enhed til en streng-array.

Overvej følgende eksempel, hvor vi har konverteret listen over strenge til en strengmatrix.

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;>