How to Reverse Content of StringBuffer in Java


In this example, let us see how to reverse the content of the StringBuffer with reverse method of java StringBuffer class.

Source Code

package com.beginner.examples;

public class StringBufferReverse {

	public static void main(String[] args) {
		
		StringBuffer stringBuffer = new StringBuffer("Hello World");
		
		System.out.println("Before reversing , stringBuffer is : " + stringBuffer);
		
		/*
		 * To reverse the StringBuffer use reverse() method of Java StringBuffer.
		 */
		stringBuffer.reverse();
		
		System.out.println("After reversing , stringBuffer is : " + stringBuffer);

	}

}

Output:

Before reversing , stringBuffer is : Hello World
After reversing , stringBuffer is : dlroW olleH
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments