There is a string [one, two] - the transformation String[] with the help of Arrays.toString() .
I pass this string as a parameter, and I need to convert this string to a String[] . How to do it?

  • one
    And why can't you transfer the array itself instead of its string representation? - Regent
  • Can array elements contain commas? - default locale
  • @ I.Perevoz what does the design have to do with anything? - Tsyklop
  • one
    @Regent is right. Using an intermediate entity is not logical, and can lead to errors. Where it would be better to be able to transfer this array using API's - I. Perevoz
  • one
    @defaultlocale and what's wrong with that? Array { "abc,", " def" } code str.substring(1, str.length() - 1).split(", ") successfully copes. - Regent

2 answers 2

In general, no way. If the array element contains a substring consisting of a comma and a subsequent space, then it is impossible to unequivocally restore the array elements.

An example of such a case:

 String[] a = { ", ", "," }; System.out.println(Arrays.toString(a)); String[] b = { "", "", "," }; System.out.println(Arrays.toString(b)); 

Arrays a and b contain different elements, but the output to the screen is the same:

 [, , ,] [, , ,] 

If there are no such substrings in the array elements (what you shouldn’t hope for in practice), you can do this:

 String[] array = { "ab,c", "d ef" }; String str = Arrays.toString(array); String[] data = str.substring(1, str.length() - 1).split(", "); 

Note: this code will give the correct result for the array { "ab,c", "d ef" } , while the replaceAll("\\s","").split(",") will give the wrong result.


The correct approach would be to pass the array itself, rather than its string representation. Relatively speaking, do so:

 String[] array = { "ab,c", "d ef" }; String[] data = array; 
  • It is still necessary to remove spaces from the string representation, since elements after the 1st will contain it. - I. Perevoz
  • @ I.Perevoz no, it does not need to do. In my version of the split a comma with a space. Check your code on the array String[] array = { "ab,c", "d ef" }; - you will have the wrong result. - Regent
  • I apologize, did not notice, but then it is better to use the regular schedule: ", \\ s?" - I. Perevoz
  • @ I.Perevoz is not, not better, because with her the same problem. Check split(",\\s?") On all the same my test array. - Regent
  • I agree. "?" generally can be removed. - I. Perevoz

Of course, this option will only work if the element itself cannot contain a comma and spaces.

 String[] arr = new String[]{"one","two"}; String s = Arrays.toString(arr); arr = s.substring(1,s.length()-1).replaceAll("\\s","").split(","); 

Result:

enter image description here

If, however, only commas and a space after them are taken into account, it should look like this:

 arr = s.substring(1,s.length()-1).split(",\\s"); 

At the same time, as @Regent wrote in the comments, the very concept of such a solution is quite controversial. Where it is more effective from the point of view of catching future errors to use the API with acc. method of transferring the array "as is".