Home Regularexpressions Interchanging String using RegularExpression



Interchanging String using RegularExpression
Posted on: August 26, 2008 at 12:00 AM
This Example describe the way to split a String and interchange the index of 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

Related Tags for Interchanging String using RegularExpression:
cstringiosplitexpressionchangeusingintthisindexexampletoexpressexamssieitdeslihangsplulinmnttresrcdescribexaxampsspessishamplpresspreandarstrxpscrssriringthstpleplprndono


More Tutorials from this section

Ask Questions?    Discuss: Interchanging String using RegularExpression  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.