📜  Java中的 StringBuilder reverse() 示例

📅  最后修改于: 2022-05-13 01:54:41.297000             🧑  作者: Mango

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

参考:
https://docs.oracle.com/javase/10/docs/api/java Java()