📜  Java中的 StringBuilder length() 示例

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

Java中的 StringBuilder length() 示例

StringBuilder 类length()方法返回 StringBuilder 对象包含的字符数。此方法返回此 StringBuilder 对象当前表示的字符序列的长度。

句法:

public int length()

返回值:该方法返回 StringBuilder 对象包含的字符序列的长度

下面的程序演示了 StringBuilder 类的 length() 方法:

示例 1:

// Java program to demonstrate
// the length() 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());
  
        // get length of StringBuilder object
        int length = str.length();
  
        // print length
        System.out.println("length of String = "
                           + length);
    }
}
输出:
String = WelcomeGeeks
length of String = 12

示例 2:

// Java program to demonstrate
// the length() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder
            str
            = new StringBuilder("India is Great");
  
        // print string
        System.out.println("String = "
                           + str.toString());
  
        // get length of StringBuilder object
        int length = str.length();
  
        // print length
        System.out.println("length of String = "
                           + length);
    }
}
输出:
String = India is Great
length of String = 14

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