Friday, October 5, 2012

[JAVA] Java Split Function, Examples









JAVA FUNCTION - SPLIT


Is a function that returns an array of strings by regular expressions.
Second parameter limit the value to the number of options you can control.

public String [] split(String  regex, int limit)


Special character
String str="1+2+10+15";
String splitted[]=str.split("[+]");
for (String value : splitted)
 System.out.println(value);

// RESULT
// 1
// 2
// 10
// 15


Blank value
String str="1,,3,";

String splitted[]=str.split(",");
for (String value : splitted)
 System.out.println(value);
// RESULT
// 1
// 3

String splitted[]=str.split(",",-1);
for (String value : splitted)
 System.out.println(value);

// RESULT
// 1
// 
// 3
//

No comments:

Post a Comment