Home Regularexpressions Interchanging String using RegularExpression
Questions:Ask|Latest

Related Tutorials


 
 

Share on Google+Share on Google+

Interchanging String using RegularExpression

Advertisement
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
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 26, 2008

Related Tutorials

Ask Questions?    Discuss: Interchanging String using RegularExpression  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com