Horje
Check if a String Contains any Whitespace Characters using JavaScript

In this article, we are given a string str, you need to find whether the given string contains whitespace characters or not in JavaScript. Whitespaces can occur at any position whether at the beginning or in between or at the end of the string.

Example 1:

Input:
str = Hi I am a Geek
Output:
The string contains whitespace characters
Explanation:
In the input string "Hi I am a geek" there are 4 whitespace characters which is shown by "_" :
Hi_I_am_a_Geek
So we return "The string contains whitespace characters"

Example 2:

Input:
str = helloWorld
Output:
The string does not contains whitespace characters

Here are some common approaches:

Using regular expressions

To determine whether a string contains whitespace characters we can make use of regular expressions. A regular expression (regex) is a sequence of characters that define a search pattern. The “\s” metacharacter is used to match the whitespace character.

Example: Below is the implementation of the approach

JavaScript
function checkWhitespace(str) {
    return /\s/.test(str);
}
let str = "Hi I am a Geek";
if (checkWhitespace(str)) {
    console.log(
        "The string contains whitespace characters."
    );
} else {
    console.log(
        "The string does not contain whitespace characters."
    );
}

Output
The string contains whitespace characters.

Using JavaScript Sets

  • Create a new Set which contains whitespace characters i.e. space, \n, \t.
  • Iterate over the string and check whether the set contains any character of the string in it.
  • If it contains any character of the string it means that the string contains whitespace characters.
  • Return true else return false.

Example: Below is the implementation of the approach

JavaScript
function checkWhitespace(str) {
    let whitespace = new Set([" ", "\t", "\n"]);
    for (let i = 0; i < str.length; i++) {
        if (whitespace.has(str[i])) {
            return true;
        }
    }
    return false;
}

let str = "GeeksforGeeks";
if (checkWhitespace(str)) {
    console.log(
        "The string contains whitespace characters."
    );
} else {
    console.log(
        "The string does not contain whitespace characters."
    );
}

Output
The string does not contain whitespace characters.

Using for loop

In this we will iterates over each character in the string using a for loop and checks if character is a whitespace character or not. If its whitespace character then we will return true else return false.

Example: In this example we checks if a string contains any whitespace characters by iterating through each character, returning true if a whitespace character is found, else false.

JavaScript
function containsWhitespace(str) {
    for (let i = 0; i < str.length; i++) {
        if (str[i] === " ") {
            return true; // Found a whitespace character
        }
    }
    return false; // No whitespace characters found
}

let str = "GeeksFor  Geeks";

console.log;
if (containsWhitespace(str)) {
    console.log(
        "The string contains whitespace characters"
    );
} else {
    console.log(
        "The string does not contains whitespace characters"
    );
}

Output
The string contains whitespace characters

Using String.prototype.includes

The String.prototype.includes method can be used to determine if a string contains a specific substring. To check for whitespace characters, we can look for common whitespace characters such as space, tab (\t), and newline (\n).

Example: Below is the implementation of this approach

JavaScript
function checkWhitespace(str) {
    return str.includes(' ') || str.includes('\t') || str.includes('\n');
}

let str = "Hi I am a Geek";

if (checkWhitespace(str)) {
    console.log("The string contains whitespace characters.");
} else {
    console.log("The string does not contain whitespace characters.");
}

Output
The string contains whitespace characters.

Using Array.prototype.some

In this approach, we will create a function that uses the some method to iterate through each character in the string and check if it is a whitespace character.

Example: In this example, we will use the some method to check each character in the string.

JavaScript
function containsWhitespace(str) {
    return [...str].some(char => char === ' ' || char === '\t' || char === '\n');
}

let str1 = "Hi I am a Geek";
let str2 = "GeeksforGeeks";

if (containsWhitespace(str1)) {
    console.log("The string contains whitespace characters.");
} else {
    console.log("The string does not contain whitespace characters.");
}

if (containsWhitespace(str2)) {
    console.log("The string contains whitespace characters.");
} else {
    console.log("The string does not contain whitespace characters.");
}
// Nikunj Sonigara

Output
The string contains whitespace characters.
The string does not contain whitespace characters.

Using String.prototype.match

The String.prototype.match method searches a string for a match against a regular expression and returns the matches as an array. If the array is not null, it means there are whitespace characters in the string.

Example: Below is the implementation of this approach:

JavaScript
function containsWhitespace(str) {
    return str.match(/\s/) !== null;
}

let str1 = "Hi I am a Geek";
let str2 = "helloWorld";

if (containsWhitespace(str1)) {
    console.log("The string contains whitespace characters.");
} else {
    console.log("The string does not contain whitespace characters.");
}

if (containsWhitespace(str2)) {
    console.log("The string contains whitespace characters.");
} else {
    console.log("The string does not contain whitespace characters.");
}

Output
The string contains whitespace characters.
The string does not contain whitespace characters.

Using String.prototype.search

The search method can be used with a regular expression to find if there’s any whitespace character in the string.

Example: Below is the implementation of this approach:

JavaScript
function containsWhitespace(str) {
    return str.search(/\s/) !== -1;
}

let str1 = "Hi I am a Geek";
let str2 = "helloWorld";

console.log(containsWhitespace(str1) ? "The string contains whitespace characters." : "The string does not contain whitespace characters.");
console.log(containsWhitespace(str2) ? "The string contains whitespace characters." : "The string does not contain whitespace characters.");

Output
The string contains whitespace characters.
The string does not contain whitespace characters.

Using Array.prototype.filter

Convert the string to an array and use filter to check for whitespace characters.

Example: Below is the implementation of this approach:

JavaScript
function containsWhitespace(str) {
    return str.split('').filter(char => char === ' ' || char === '\t' || char === '\n').length > 0;
}

let str1 = "Hi I am a Geek";
let str2 = "helloWorld";

console.log(containsWhitespace(str1) ? "The string contains whitespace characters." : "The string does not contain whitespace characters.");
console.log(containsWhitespace(str2) ? "The string contains whitespace characters." : "The string does not contain whitespace characters.");

Output
The string contains whitespace characters.
The string does not contain whitespace characters.



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Sort an Array which Contain 1 to n Values JavaScript Program to Sort an Array which Contain 1 to n Values
JavaScript Program to Construct an Array from its pair-sum Array JavaScript Program to Construct an Array from its pair-sum Array
JavaScript Program to Find LCM JavaScript Program to Find LCM
JavaScript Program to find the Index of the First Occurrence of a Substring in a String JavaScript Program to find the Index of the First Occurrence of a Substring in a String
Less.js Options Source Map Options Less.js Options Source Map Options

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11