Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

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