在 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

更新于: 09-Sep-2020

4K+ 浏览量

开启你的职业生涯

通过完成本课程获得认证

开始
广告
© . All rights reserved.