在 JavaScript 中检查字符串是否包含空白字符?
要检查字符串中是否有空白字符,请使用 indexOf(‘ ’) 的概念。以下是代码:-
示例
function stringHasTheWhiteSpaceOrNot(value){
return value.indexOf(' ') >= 0;
}
var whiteSpace=stringHasTheWhiteSpaceOrNot("MyNameis John");
if(whiteSpace==true){
console.log("The string has whitespace");
} else {
console.log("The string does not have whitespace");
}要运行以上程序,您需要使用以下命令 -
node fileName.js.
在此处,我的文件名是 demo108.js。
输出
它将产生以下输出 -
PS C:\Users\Amit\JavaScript-code> node demo108.js The string has whitespace
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP