logo

Kā pārvērst virkni par virkņu masīvu Java?

Java valodā a Stīga ir objektu kas attēlo rakstzīmju secību. Lai izmantotu Strings, mums ir jāimportē Stīga punktā definētā klase java.lang iepakojums. The Virkņu masīvs ir masīvs virknes ar fiksētu garumu. Šajā sadaļā mēs uzzināsim kā pārvērst virkni virkņu masīvā .

Piezīme. Java virknes ir nemainīgi objekti, t.i., virknes vērtību nevar mainīt, kad tā ir izveidota.

Virknes pārveidošana par virkņu masīvu

Ir četri veidi, kā pārvērst virkni virkņu masīvā Java:

datoru veidi
  1. Izmantojot String.split() metodi
  2. Izmantojot Pattern.split() metodi
  3. String[ ] pieejas izmantošana
  4. Izmantojot toArray() metodi

Izmantojot String.split() metodi

The String.split() metode tiek izmantota, lai sadalītu virkni atsevišķās virkņu entītijās, pamatojoties uz doto norobežotāju (baltstarpu vai citiem simboliem). Mēs varam saglabāt šīs entītijas tieši virkņu masīvā.

Apskatīsim šādu piemēru, kur mēs izmantojam String.split() metodi, lai pārvērstu virkni virkņu masīvā.

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

Apskatīsim šādu piemēru, kur mēs sadalām virkni masīvā, izmantojot atdalītāju kā atstarpi.

javascript ielādes skripts

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

Izvade:

Kā pārvērst virkni par virkņu masīvu Java

Izmantojot toArray() metodi

Mēs varam arī pārvērst virkni par virknes masīvu, izmantojot toArray() metode Saraksts klasē. Tas izmanto sarakstu ar tipa String kā ievadi un pārvērš katru entītiju virknes masīvā.

Apsveriet šādu piemēru, kurā mēs esam pārveidojuši virkņu sarakstu virkņu masīvā.

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