// 判断空字符串的示例代码
// 方法 1: 使用严格相等运算符
function isEmptyString(str) {
return str === ''; // 如果字符串严格等于空字符串,则返回 true
}
console.log(isEmptyString('')); // true
console.log(isEmptyString('hello')); // false
// 方法 2: 使用 String.prototype.trim() 去除空白字符后再判断
function isEmptyOrWhitespace(str) {
return str.trim() === ''; // 如果去除空白字符后是空字符串,则返回 true
}
console.log(isEmptyOrWhitespace(' ')); // true
console.log(isEmptyOrWhitespace(' hello ')); // false
=== 来判断字符串是否为空字符串 ''。这种方法只能检测严格意义上的空字符串,不能处理包含空白字符的情况。trim() 方法去除字符串中的空白字符(如空格、制表符等),然后再判断是否为空字符串。这种方法可以检测出只包含空白字符的字符串。上一篇:js 判断字符串为空
下一篇:js 字符串转布尔
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站