Navigate back to the homepage

how to check if a string contains only whitespace in typescript?

Infinitbility
TypeScript
July 28th, 2022 · 1 min read
how to check if a string contains only whitespace in typescript?

Check if a string contains only whitespace in typescript

To check if a string contains only whitespace in typescript, you can use the trim() method with the if...else statement it will go in else if the string contains blank spaces.

1// ✅ Check if a string contains only whitespace Using `trim()` method
2(" ".trim()) ? "String contain character" : "String contain only spaces"; // 👉️ 'String contain only spaces'

Today, I’m going to show you How do I check if a string contains only whitespace in typescript, as above mentioned here, I’m going to use the trim() method with the if...else statement it will print in the else statement if the string contains only blank spaces.

Let’s start today’s tutorial how do you check if a string contains only whitespace in typescript?

Here, I have taken example two string one have only empty space and one have some words.

1const str1: string = " ";
2const str2: string = " Infint bility ";
3
4// ✅ Check str1 contain only whitespace
5if(str1.trim()){
6 console.log("str1 contain character");
7} else {
8 console.log("str1 contain only space");
9}
10
11// ✅ Check str2 contain only whitespace
12if(str2.trim()){
13 console.log("str2 contain character");
14} else {
15 console.log("str2 contain only space");
16}
17
18// ✅ Output
19// 👉️ 'str1 contain only space'
20// 👉️ 'str2 contain character'

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to get length of numbers in typescript?

how to get length of numbers in typescript?

TypeScript, get the length of numbers example with a step-by-step guide.

July 28th, 2022 · 1 min read
how to get length of object in typescript?

how to get length of object in typescript?

TypeScript, get the length of the object example with a step-by-step guide.

July 28th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]