In this article, we will discuss StringBuffer’s reverse() method which is used to reverse the contents of invoking StringBuffer object
1. StringBuffer’s reverse() method:
- This StringBuffer method is used to reverse the sequence of character of the invoking StringBuffer object
- Note: sometimes it is used to convert string in reverse-order
- But that String need to be converted to StringBuffer using append() method
- and later when reversing is done, again converting StringBuffer to String
1.1 Method Signature:
public StringBuffer reverse();
1.2 Returns:
- Return the StringBuffer object with reversed contents (or say in reverse-order)
2. Examples on reverse() method:
- To reverse StringBuffer contents
StringBufferReverseMethod.java
package in.bench.resources.stringbuffer.methods;
public class StringBufferReverseMethod {
public static void main(String[] args) {
// StringBuffer - 1
StringBuffer sb1 = new StringBuffer("Mumbai Indians");
// to reverse StringBuffer contents
sb1.reverse();
System.out.println("1. StringBuffer reverse contents : "
+ sb1);
// StringBuffer - 2
StringBuffer sb2 = new StringBuffer();
sb2.append("Made in India"); // append string
// to reverse StringBuffer contents
sb2.reverse();
System.out.println("2. StringBuffer reverse contents : "
+ sb2);
}
}
Output:
1. StringBuffer reverse contents : snaidnI iabmuM
2. StringBuffer reverse contents : aidnI ni edaM
Related Articles:
- Java – StringBuffer class
- Java – StringBuffer append() method (13)
- Java – StringBuffer capacity() method
- Java – StringBuffer charAt(int index) method
- Java – StringBuffer delete(int start, int end) method
- Java – StringBuffer deleteCharAt(int index) method
- Java – StringBuffer ensureCapacity(int minimumCapacity) method
- Java – StringBuffer indexOf() method (2)
- Java – StringBuffer insert() method (12)
- Java – StringBuffer lastIndexOf() method (2)
- Java – StringBuffer length() method
- Java – StringBuffer replace(int start, int end, String str) method
- Java – StringBuffer reverse() method
- Java – StringBuffer substring(int start, int end) method
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!