Split same pattern String in java

This is simple but useful tutorials for begainers and intermediate .

If you have same pattern string then you can split and store in string array. like this

String strtest1="software android developer job software java developer job software web developer job";

others may be in user when you get logical condition string from database like this:

first method:

              String s  = "if x=7 else if x=6 else if x>5 else";
String[] parts = test.replaceAll("\\s*(?:if|else)\\s*", "").split("(?<=\\d)");

[x=7, x=6, x>5]

         String strtest2="if x==7 then y=4 else if x<6 then y=12else if x>5 then y=0 else";

                ArrayList<String> list_array = new ArrayList<String>();
   String[] string_array = test.split("else");
   for(int i = 0; i < string_array.length; i++){
        list_array.add((string_array[i].replace("if"," ")).trim());
   System.out.println("Method 2= "+list_array);

Output Looks Like:
Method 2= [x==7 then y=4, x<6 then y=12, x>5 then y=0]
Happy Coding!!!

Split same pattern String in java Split same pattern String in java Reviewed by D Kshetri on 11:03 PM Rating: 5

No comments:

Powered by Blogger.