Syntax error How to convert comma seperated java string to an array.

How to convert comma seperated java string to an array.



Yes, use String.split() method to do it. See the example below −

Example

public class Tester {
   public static void main(String[] args) {
      String text = "This,is,a,comma,seperated,string.";
      String[] array = text.split(",");
      for(String value:array) {
         System.out.print(value + " ");
      }
   }
}

Output

This is a comma seperated string.
Updated on: 2020-02-24T12:15:58+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements