Java String Split Example
Chapter:
String Handling
Last Updated:
20-08-2016 18:29:57 UTC
Program:
/* ............... START ............... */
public class JavaStringSplitExample {
public static void main(String args[]) {
String str = "jan-feb-march";
String[] temp;
String delimeter = "-";
temp = str.split(delimeter);
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
System.out.println("");
str = "jan.feb.march";
delimeter = "\\.";
temp = str.split(delimeter);
}
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
System.out.println("");
temp = str.split(delimeter, 2);
for (int j = 0; j < temp.length; j++) {
System.out.println(temp[j]);
}
}
}
}
/* ............... END ............... */
Output
jan
feb
march
jan
jan
feb.march
feb.march
jan
feb.march
Notes:
-
split(string) method is used to splits a string into a number of substrings
Tags
String Split, Java