How to reverse a string in Java



This example shows how to reverse a String using the StringUtils class in the Apache Commons Lang library. It also shows how to reverse parts of a String that are delimited by a certain delimiter.

The StringUtils class has one method for reversing a String, reverse(), and one method for reversing with delimiter, reverseDelimited(). The first one simply takes the String to be reversed as parameter, and the second one takes the string to be reversed as first parameter and the delimiter as the second parameter of type char. The example below show how to use them:

First we reverse String ‘s’ and ‘s1’ without any delimiters. Then we reverse ‘s1’ with delimiter. The whole String gets reversed. Last we reverse ‘s2’ with delimiter. Here only 1 and 4 gets reversed. The output from the code above is:







Search for more Java info on this site here:
Custom Search


Please type any questions here.