[an error occurred while processing this directive]

String Reverse Using StringUtils

                         

In this example we are going to reverse a given string using StringUtils api. In this example we are reversing a string and the reversed string  is delimited by a specific character.

The org.apache.commons.lang.StringUtils class extends Object class and defines certain words related to String handling such as null for null,"" for a zero-length string, ' ' for space characters, Charecter.isWhitespace(char) for whitespace and String.trim() for trim.

The methods used:

reverse(String str): This method is used to reverse the order of a buffered string. It returns a null value if no string is passed i.e StringUtils.reverse(null) = null or StringUtils.reverse("") = "" otherwise reverse order will returns e.g. StringUtils.reverse("bat") = "tab".

reverseDelimited(String str,char separatorChar): This method is used to reverse a String that is delimited by a specific character like "". The strings between the delimiters will not reverse. In this method "str" is string to reverse and "separatorChar" is the separator character.


 The code of the program is given below:

import org.apache.commons.lang.StringUtils;
 
public class StringReverseUsingStringUtils 
{    
    public static void main(String[] args)
    {
    String string = "Hi, How R YOU?";         
    String reverse = StringUtils.reverse(string);
    String delimitedReverse = StringUtils.
reverseDelimited
(string, ' ');               
    System.out.println("\nThe original String: " + string);
    System.out.println("The reversed string: " + reverse);
    System.out.println("The delimited Reverse string: " 
+ delimitedReverse);
    }
}

The output of the program is given below:

C:\rajesh\kodejava>javac StringReverseUsingStringUtils.java
C:\rajesh\kodejava>java StringReverseUsingStringUtils
The original String: Hi, How R YOU?
The reversed string: ?UOY R woH ,iH
The delimited Reverse string: YOU? R How Hi,

Download this example.

                         

[an error occurred while processing this directive]