下面是一个示例代码,在该示例中使用了length()方法来获取字符串的长度:
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
int length = str.length();
System.out.println("The length of the string is: " + length);
}
}
在上面的示例中,我们使用了length()方法来获取字符串str的长度,并将结果存储在变量length中。然后将长度打印到控制台。这样可以更高效地获取字符串的长度,而不需要手动计算字符的数量。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>