The String split( ) method accepts a regular expression that describes a delimiter and uses it to chop the string into an array of Strings:
String str = "Apple,Orange,Banana,Mango,pinaple ";
String[] results = str.split(",");
The split() method on the String class accepts a regular expression as its only parameter, and will return an array of String objects split according to the rules of the passed-in regular expression. This makes parsing a comma-separated string an easy task. In this phrase, we simply pass a comma into the split() method, and we get back an array of strings containing the comma-separated data. So the results array in our phrase would contain the following content:
results[0] = Apple
results[1] = Orange
results[2] = Banana
results[3] = Mango
results[4] = pinaple
Example:
/**
*
* @author JavaHotSpot
*/
public class StringSplit
{
public static void main(String args[])
{
String str = "Apple,Orange,Banana,Mango,pinaple";
String[] results=str.split(",");
for(int i=0;i<results.length;i++)
{
System.out.println(results[i]);
}
}
}
Output:
Apple
Orange
Banana
Mango
pinaple
Related Articles:
File Class
StringBuffer
StringTokenizer
Scanner
String Class
Wrapper Class
Related Articles:
File Class
StringBuffer
StringTokenizer
Scanner
String Class
Wrapper Class