How do I reverse a string, words or sentences?
Category: commons.lang, viewed: 10582 time(s).
package org.kodejava.example.commons.lang;
import org.apache.commons.lang.StringUtils;
public class StringReverseExample
{
public static void main(String[] args)
{
// We have an original string here that we'll need to reverse.
String words = "The quick brown fox jumps over the lazy dog";
// Using StringUtils.reverse we can reverse the string letter by letter.
String reversed = StringUtils.reverse(words);
// Now we want to reverse per word, we can use
// StringUtils.reverseDelimited() method to do this.
String delimitedReverse = StringUtils.reverseDelimited(words, ' ');
// Print out the result
System.out.println("Original: " + words);
System.out.println("Reversed: " + reversed);
System.out.println("Delimited Reverse: " + delimitedReverse);
}
}
Here is the result:
Original: The quick brown fox jumps over the lazy dog
Reversed: god yzal eht revo spmuj xof nworb kciuq ehT
Delimited Reverse: dog lazy the over jumps fox brown quick The
Can't find what you are looking for? Join our
FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!