判断字符串是否为空,常见有几种情况:空字符串、空白字符串、null。不同语言略有差异,下面给你常见语言的判断方式。
"")" ")String s = "";
// 是否为 null
if (s == null) {
System.out.println("null");
}
// 是否为空字符串
if (s != null && s.isEmpty()) {
System.out.println("空字符串");
}
// 是否为空白字符串(Java 11+)
if (s != null && s.isBlank()) {
System.out.println("空白字符串");
}
let s = "";
// 是否为空字符串
if (s === "") {
console.log("空字符串");
}
// 是否为空或空白
if (!s || s.trim() === "") {
console.log("空或空白字符串");
}
s = ""
# 是否为 None
if s is None:
print("None")
# 是否为空字符串
if s == "":
print("空字符串")
# 是否为空或空白
if not s or s.strip() == "":
print("空或空白字符串")
string s = "";
// 是否为 null 或空字符串
if (string.IsNullOrEmpty(s))
{
Console.WriteLine("null 或空字符串");
}
// 是否为 null、空或空白
if (string.IsNullOrWhiteSpace(s))
{
Console.WriteLine("null、空或空白字符串");
}
s := ""
if s == "" {
fmt.Println("空字符串")
}
// 判断是否只包含空白
if strings.TrimSpace(s) == "" {
fmt.Println("空白字符串")
}
✅ 最常用组合判断
if (str == null || str.trim().isEmpty()) {
// 视为空
}
✅ 优先级建议
null / None如果你能告诉我具体使用哪种语言或具体业务场景(如表单校验、接口参数),我可以给你更精准的写法。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。