StringBuilder的reverse()方法用於反轉StringBuilder中的字符。該方法有助於將此字符序列替換為相反的序列。
用法:
public java.lang.AbstractStringBuilder reverse()
返回值:
反轉字符後,此方法返回StringBuilder對象。
以下程序說明了java.lang.StringBuilder.replace()方法:
示例1:
// Java program to demonstrate
// the reverse() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("WelcomeGeeks");
// print string
System.out.println("String = "
+ str.toString());
// reverse the string
StringBuilder reverseStr = str.reverse();
// print string
System.out.println("Reverse String = "
+ reverseStr.toString());
}
}
輸出:
String = WelcomeGeeks Reverse String = skeeGemocleW
示例2:
// Java program to demonstrate
// the reverse() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("AAAABBBCCCC");
// print string
System.out.println("String = "
+ str.toString());
// reverse the string
StringBuilder reverseStr = str.reverse();
// print string
System.out.println("Reverse String = "
+ reverseStr.toString());
}
}
輸出:
String = AAAABBBCCCC Reverse String = CCCCBBBAAAA
相關用法
- Java StringBuilder length()用法及代碼示例
- Java StringBuilder Class用法及代碼示例
- Java StringBuilder codePointCount()用法及代碼示例
- Java StringBuilder deleteCharAt()用法及代碼示例
- Java StringBuilder codePointBefore()用法及代碼示例
- Java StringBuilder ensureCapacity()用法及代碼示例
- Java StringBuilder capacity()用法及代碼示例
- Java StringBuilder delete()用法及代碼示例
- Java StringBuilder getChars()用法及代碼示例
- Java StringBuilder charAt()用法及代碼示例
- Java StringBuilder subSequence()用法及代碼示例
- Java StringBuilder replace()用法及代碼示例
- Java StringBuilder codePointAt()用法及代碼示例
- Java StringBuilder setLength()用法及代碼示例
- Java StringBuilder setCharAt()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuilder reverse() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。