Java.lang.StringBuffer.reverse()是一種內置方法,用於反轉StringBuffer中的字符。該方法導致此字符序列被序列的相反字符替換。
用法:
public StringBuffer reverse()
參數:該方法不帶任何參數。
返回值:反轉字符後,該方法返回StringBuffer。
例子:
Input: StringBuffer = GeeksforGeeks Output = !skeegrofskeeG Input: StringBuffer = Hello World Output = !dlroW olleH
以下程序說明了java.lang.StringBuffer.reverse()方法:
示例1:
// Java praogram to illustrate the
// java.lang.StringBuffer.reverse()
import java.lang.*;
public class Test {
public static void main(String args[])
{
StringBuffer sbf = new StringBuffer("Geeksforgeeks!");
System.out.println("String buffer = " + sbf);
// Here it reverses the string buffer
sbf.reverse();
System.out.println("String buffer after reversing = " + sbf);
}
}
輸出:
String buffer = Geeksforgeeks! String buffer after reversing = !skeegrofskeeG
示例2:
// Java praogram to illustrate the
// java.lang.StringBuffer.reverse()
import java.lang.*;
public class Test {
public static void main(String args[])
{
StringBuffer sbf = new StringBuffer("1 2 3 4 5 6 7 8 9 10");
System.out.println("String buffer = " + sbf);
// Here it reverses the string buffer
sbf.reverse();
System.out.println("String buffer after reversing = " + sbf);
}
}
輸出:
String buffer = 1 2 3 4 5 6 7 8 9 10 String buffer after reversing = 01 9 8 7 6 5 4 3 2 1
相關用法
- Java StringBuffer append()用法及代碼示例
- Java StringBuffer substring()用法及代碼示例
- Java StringBuffer lastIndexOf()用法及代碼示例
- Java StringBuffer replace()用法及代碼示例
- Java StringBuffer delete()用法及代碼示例
- Java StringBuffer appendCodePoint()用法及代碼示例
- Java StringBuffer deleteCharAt()用法及代碼示例
- Java StringBuffer codePointAt()用法及代碼示例
- Java StringBuffer codePointCount()用法及代碼示例
- Java StringBuffer setCharAt()用法及代碼示例
- Java StringBuffer codePointBefore()用法及代碼示例
- Java StringBuffer indexOf()用法及代碼示例
- Java StringBuffer offsetByCodePoints()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuffer ensureCapacity()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 StringBuffer reverse() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。