Java StringBuffer example to reverse the string or word in a StringBuffer


public StringBuffer reverse()
Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed. Let n be the character length of this character sequence (not the length in char values) just prior to execution of the reverse method. Then the character at index k in the new character sequence is equal to the character at index n-k-1 in the old character sequence.
Note that the reverse operation may result in producing surrogate pairs that were unpaired low-surrogates and high-surrogates before the operation. For example, reversing "\uDC00\uD800" produces "\uD800\uDC00" which is a valid surrogate pair.

Program:
package com.candidjava;

public class StringBufferReverse 
{
    public static void main(String[] args) 
    {
        StringBuffer sb = new StringBuffer("Student");
        sb.reverse();
        System.out.println(sb);
    }
}

Output:
tneduts

Description:
The given string "student" is being reversed.

Returns:
a reference to this object.


Related Post

Comments


©candidjava.com