logo

Како претворити стринг у низ низова у Јави?

У Јави, а Низ је објекат који представља низ знакова. Да бисмо користили Стрингове, морамо да увеземо Низ класа дефинисана у јава.ланг пакет. Тхе Низ низова је низ жица са фиксном дужином. У овом одељку ћемо научити како претворити стринг у низ стрингова .

Напомена: Јава стрингови су непроменљиви објекти, тј. вредност стринга се не може променити када се креира.

Претварање стринга у низ низова

Постоје четири начина да конвертујете стринг у низ низова у Јави:

како поновити у пхотосхопу
  1. Коришћење Стринг.сплит() методе
  2. Коришћење методе Паттерн.сплит().
  3. Коришћење Стринг[ ] приступа
  4. Коришћење методе тоАрраи().

Коришћење Стринг.сплит() методе

Тхе Стринг.сплит() метода се користи да се стринг подели на појединачне низове на основу датог граничника (размака или других симбола). Ове ентитете можемо ускладиштити директно у низу стрингова.

Хајде да размотримо следећи пример где користимо метод Стринг.сплит() да конвертујемо стринг у низ стрингова.

ТестСплитМетход.јава

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

Хајде да размотримо следећи пример где делимо стринг у низ користећи граничник као размак.

цсс граница

СплитМетходОфПаттернЦласс.јава

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

Излаз:

Како претворити стринг у низ низова у Јави

Коришћење методе тоАрраи().

Такође можемо да конвертујемо Стринг у низ низова користећи тоАрраи() методом Листа класа. Узима листу типа Стринг као улаз и конвертује сваки ентитет у низ стрингова.

Размотрите следећи пример где смо конвертовали листу стрингова у низ стрингова.

СтрингЛисттоАрраи.јава

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