当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java StringBuilder reverse()用法及代码示例


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

参考文献:



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 StringBuilder reverse() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。