1.

How to split String with Comma (,) in Java

Answer»

The split() method is used to split a string on the basis of regular expression. The first PARAMETER is the same regular expression, whereas if the second parameter is zero, it RETURNS all the strings matching the Regular Expression.

Our sample string:

The TV, and the remote

To split the string with comma, the following is the EXAMPLE:

public class Example {  public static void main(String[] args) {      String s = "The TV, and the remote";      System.out.println("Initial String = "+s);      String[] str = s.split("[,]", 0);      System.out.println("\nSplitted string: ");      for(String val: str){        System.out.println(val);      }   } }

The OUTPUT:

Initial String = The TV, and the remote Splitted string: The TV and the remote


Discussion

No Comment Found