Interchanging String using RegularExpression

This Example describe the way to split a String and interchange the index of string using Regularexpression.

Interchanging String using RegularExpression

Interchanging String using RegularExpression

     

This Example describe the way to split a String and interchange the index of string using Regularexpression. The steps involved in interchanging a String are described below:

String parg = "For some college -Presidents want law- revisited saying age limit- has not curbed-that problems on campuses":- Defines a string that is to be splitted after (-) expression.

String[] token = null:-Declares an array named token in which the splitted string is to be stored.

String splitPoint = "-":-Declares the point from where the string is to be splitted.

sentence.split(splitPoint):-This method splits the string from the point where it finds (-)expression.

retival = token[1] + " " + token[0] +""+ token[4] +""+ token[3]:-This method is generally for the displaying tokens in the order which you want.

SplittingString_Interchanging.java

import java.util.regex.*;
public class SplittingString_Interchanging {
 public static void main(String args[]) {
  String parg = "For some college -Presidents want law- revisited"+
  " saying age limit- has not curbed-that problems on campuses";
  interchange(parg);
  }
  public static String interchange(String sentence) {
 String retival = null;
  String[] token = null;
  String splitPoint = "-";
  token = sentence.split(splitPoint);
  if (token == null) {
  String msg = " Not Matching: pattern:" + sentence
  "\r\n regex: " + splitPoint;
  else {
  retival = token[1" " + token[0+""+ token[4+""+ token[3];
  System.out.println("BEFORE SPLITTING: " + sentence);
  System.out.println("AFTER SPLITTING: " + retival + "\n");
  }
  return retival;
  }
}

Output of the program
BEFORE SPLITTING: For some college -Presidents want law- revisited
saying age limit- has not curbed-that problems on campuses
============================================
AFTER SPLITTING: Presidents want law For some college
that problems on campuses has not curbed

Download Source code