string的length()方法返回字符串中unicode字符的数量,而getbytes().length返回特定编码下的字节数;2. 处理ascii字符时两者结果相同,但对中文等非ascii字符,因编码不同(如utf-8中一个中文占3字节,gbk中占2字节),字节数通常大于字符数;3. 应根据需求选择:若需字符个数(如字符串截取、遍历)使用length(),若涉及存储或传输中字节限制则使用getbytes().length;4. 为避免null调用length()抛出nullpointerexception,应进行判空检查,可通过if判断、三元运算符、objects.requirenonnullelse(java 9+)、optional(java 8+)或apache commons lang的stringutils.length()实现安全获取长度。

String的length()方法是Java中获取字符串长度最直接的方式。它返回的是字符串中字符的数量,是一个整数值。
解决方案
在Java中,获取字符串长度非常简单,只需调用String对象的length()方法即可。下面是一些示例和注意事项:
立即学习“Java免费学习笔记(深入)”;
public class StringLengthExample {
public static void main(String[] args) {
String str1 = "Hello World";
int length1 = str1.length();
System.out.println("Length of '" + str1 + "': " + length1); // 输出:11
String str2 = "";
int length2 = str2.length();
System.out.println("Length of empty string: " + length2); // 输出:0
String str3 = "你好世界"; // 包含中文
int length3 = str3.length();
System.out.println("Length of '" + str3 + "': " + length3); // 输出:4,注意这里是字符数,不是字节数
String str4 = null;
// int length4 = str4.length(); // 这里会抛出NullPointerException,需要避免
if (str4 != null) {
int length4 = str4.length();
System.out.println("Length of '" + str4 + "': " + length4);
} else {
System.out.println("String is null, cannot get length.");
}
}
}需要注意的是,
length()
length()
另外,对
null
length()
NullPointerException
String的length()方法和getBytes().length的区别?
length()
getBytes().length
public class StringByteLengthExample {
public static void main(String[] args) {
String str = "你好abc";
int charLength = str.length();
System.out.println("Character length: " + charLength); // 输出:5
int byteLengthUTF8 = str.getBytes("UTF-8").length;
System.out.println("Byte length in UTF-8: " + byteLengthUTF8); // 输出:9 (每个中文占3个字节)
int byteLengthGBK = str.getBytes("GBK").length;
System.out.println("Byte length in GBK: " + byteLengthGBK); // 输出:7 (每个中文占2个字节)
String strAscii = "abc";
int asciiCharLength = strAscii.length();
System.out.println("Character length (ASCII): " + asciiCharLength); // 输出:3
int asciiByteLengthUTF8 = strAscii.getBytes("UTF-8").length;
System.out.println("Byte length in UTF-8 (ASCII): " + asciiByteLengthUTF8); // 输出:3
}
}可以看到,对于纯ASCII字符串,
length()
getBytes("UTF-8").length什么时候应该用length(),什么时候应该用getBytes().length?
使用 length()
使用 getBytes().length
举个例子,如果需要限制用户输入的字符串长度不超过10个字符,应该使用
length()
getBytes("UTF-8").length如何避免String为null时调用length()方法报错?
处理
null
null
length()
NullPointerException
判空检查:
这是最常见和最直接的方法。在使用
length()
null
String str = null;
if (str != null) {
int length = str.length();
System.out.println("Length: " + length);
} else {
System.out.println("String is null.");
}使用三元运算符:
可以使用三元运算符简化判空检查。
String str = null;
int length = (str != null) ? str.length() : 0;
System.out.println("Length: " + length); // 输出 0使用 Objects.requireNonNullElse
Java 9 引入了
Objects.requireNonNullElse
null
import java.util.Objects;
public class NullSafeLength {
public static void main(String[] args) {
String str = null;
String safeStr = Objects.requireNonNullElse(str, "");
int length = safeStr.length();
System.out.println("Length: " + length); // 输出 0
}
}使用Optional (Java 8+):
Optional
null
null
import java.util.Optional;
public class NullSafeLengthOptional {
public static void main(String[] args) {
String str = null;
int length = Optional.ofNullable(str).orElse("").length();
System.out.println("Length: " + length); // 输出 0
}
}使用StringUtils (Apache Commons Lang):
Apache Commons Lang 库提供了一个
StringUtils
length()
import org.apache.commons.lang3.StringUtils;
public class NullSafeLengthStringUtils {
public static void main(String[] args) {
String str = null;
int length = StringUtils.length(str);
System.out.println("Length: " + length); // 输出 0
}
}选择哪种方法取决于具体的场景和个人偏好。判空检查是最基本的方法,适用于所有Java版本。
Objects.requireNonNullElse
Optional
StringUtils
StringUtils.length()
以上就是java如何用String的length()获取字符串长度 java字符串长度语句的基础教程的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号