Hi Guys! Some Time, When we need to split String object in some pattern based and you want to store them in Array. It will help you a lot. I'm going to give you three examples that will help you.
Splits the string based on whitespace
public class SplitExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
String sentence="Ram lagaya jodi Andha Ek Kodhi";
String[] words=sentence.split("\\s");
//splits the string based on whitespace
for(int i=0; i<words.length; i++)
{
System.out.println(words[i]);
}
}
}
Splits the string based on £ pound Symbol for String Only
public class SplitEx2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String sentence="Ram£lagaya£jodi£Andha£Ek£Kodhi";
String[] words=sentence.split("\\£");
//splits the string based on £ pound Symbol
for(int i=0; i<words.length; i++)
{
System.out.println(words[i]);
}
}
}
Splits the string based on £ pound Symbol for all
public class SplitEx3 {
public static void main(String[] args) {
String sentence="9000£9088£null£Ram£lagaya£jodi£Andha£Ek£Kodhi";
String[] words=sentence.split("\u00a3");
for(int i=0; i<words.length; i++)
{
System.out.println(words[i]);
}
}
}