Java我怎么知道一个字符串的长度?
要获得在String对象中指定的文本的长度,我们可以使用String.length()方法。此方法返回字符串长度作为int值。字符串的长度等于此字符串对象表示的字符序列的长度。
package org.nhooo.example.lang;
public class StringLengthExample {
public static void main(String[] args) {
String name = "Nhooo";
//获取名称指定的字符串的长度
//变量。
int length = name.length();
System.out.println("Length = " + length);
}
}如果运行此代码,您将获得以下输出:
Length = 8