Warning: session_start(): open(/tmp/sess_5b1fae17d64a7a5a52f088637c79adff, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check If String Contains Whitespace In JavaScript - LearnShareIT

How To Check If String Contains Whitespace In JavaScript

How To Check If String Contains Whitespace In JavaScript

To check if string contains whitespace in JavaScript, you can use the indexOf(str) function or the includes() method. In this article we will present specific steps to do it. Let’s check it out!

String in Javascript

A string is a vital data type and information used in practice. When you need to store information in the database, more than 90% will be stored as strings.

A string is a text with one or more characters, and we usually will store it in a variable, which we will call a variable with data type String (String). All strings must be surrounded by single quotes ‘ or double quotes “. For example:

Code:

var website = "LearnShareIT.com";
var email = 'learnshareit@gmail.com';

Here is a list of JavaScript String handling methods:

  • charAt(index)
  • concat(str)
  • indexOf(str)
  • lastIndexOf(str)
  • toLowerCase()
  • toUpperCase()
  • slice(beginIndex, endIndex)
  • trim(str)

Check if string contains whitespace in JavaScript

Use indexOf() method

The indexOf(str) method in JavaScript returns the index position of the given string.

Applying the example below, we will find the position of the space string in the name string. If the return value is greater than one, the string contains space. Let’s see the example below to see how we apply it to real problems.

Code:

<!DOCTYPE html>
<html>
  <head>
    <title>LearnShareIT</title>
  </head>
  <body>
    <h1 id="name"></h1>
    <script>
      var str = "John Snow";
      document.getElementById("name").innerHTML = str;
     
      if (str.indexOf(" ") >= 0) {
        console.log("This string contains spaces");
      }

    </script>
  </body>
</html>

Output:

This String contains spaces

The program will compare the string str, which is also the string containing the name and use the indexOf() method to find the position of space in the string str. Finally it returns the value that means the string contains space. If it returns undefined, it means the string has no space.

Use includes() method

The includes() method returns true or false depending on whether an array contains a particular value among its entries.

In this way, we use the includes() method with the string to find out if the string has a space value or not. We will use includes() with a space param to look in the title string to see if yes or no. If yes, It will return true. Otherwise, it will return false.

Code:

<!DOCTYPE html>
<html>
  <head>
    <title>LearnShareIT</title>
  </head>
  <body>
    <h1 id="title"></h1>
    <script>
      var str = "Welcome to LearnShareIT";
      document.getElementById("title").innerHTML = str;
 
      if (str.includes(" ")=== true) {
        console.log("This string contains spaces");
      }
 
    </script>
  </body>
</html>

Output:

This String contains spaces

So with, both ways, we can get the same result. Both ways can check if there are spaces in the string. I hope this post is helpful to you.

Summary

To summarize, there are many ways to check if the string contains a whitespace in JavaScript. After reading this article, you know some easy ways to do it and can choose the suitable method for you. Let’s try them to get your desired results!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *