Friday 3 April 2015

Split Array with Comma ( Empty data in String)

I got an issue to split String in java when there is empty data in String after comma.

String testStr = "one,,";

Spliting  like below solved my problem

String[] result = testStr.split("\\,", -1);






1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class SplitArray {
 public static void main(String[] argv) {
  String testStr = "one,,";

  System.out.println("Original string : " + testStr);
  
  
  System.out.print("\nSplit at commas: ");
  
  String[] result = testStr.split("\\,", -1);
  
  System.out.print("Array Length : "+ result.length);
  
  for(int i=0;i<result.length; i++)
  {
   System.out.print("\n position:"+i +" "+ result[i]);
  }
  
  
 }
}