Mega Code Archive

 
Categories / Java / Regular Expressions
 

String split

public class StyleSplitExample {   public static void main(String args[]) {     String phrase1 = "but simple justice, not charity";     strengthenSentence(phrase1);     String phrase2 = "but that I love Rome more, not that I love Caesar less";     strengthenSentence(phrase2);     String phrase3 = "ask what you can do for your country, ask not what your "         + "country can do for you";     strengthenSentence(phrase3);   }   public static String strengthenSentence(String sentence) {     String retval = null;     String[] tokens = null;     String splitPattern = ",";     tokens = sentence.split(splitPattern);     if (tokens == null) {       String msg = "   NO MATCH: pattern:" + sentence           + "\r\n             regex: " + splitPattern;     } else {       retval = tokens[1] + ", " + tokens[0];       System.out.println("BEFORE: " + sentence);       System.out.println("AFTER : " + retval + "\n");     }     return retval;   } }