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 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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。