Navigate back to the homepage

Javascript remove whitespace from beginning and end of string

Infinitbility
JavaScript
January 12th, 2022 · 1 min read
Javascript remove whitespace from beginning and end of string

Hello Friends 👋,

Welcome to Infinitbility.

Every time, when we take details from users they’re a lot of chances we will not get the same like we are expecting and space or white space is one of them.

Today, we will see how we can remove white space at the start and end of the string.

Before going to the solution, let discuss the solution that will help you on which conditions.

  • The first case, the string has both ( left and right ) side useless spaces.

    ' Hello World ' => 'Hello World'

  • The second case, the string has only useless space on the left side.

    ' Hello World' => 'Hello World'

  • The third case, the string has only useless space on the right side.

    'Hello World ' => 'Hello World'

The below solution will help you in all the above cases and it will not affect your important or original string like Hello world.

Now, move to a solution.

JavaScript provides a trim() method to remove space from the beginning and end of the string.

Let understand with an example.

1let exampleString = " Hello world ";
2
3console.log("exampleString => ", exampleString);
4
5exampleString.trim();

Output

Javascript trim example
Javascript trim example

Thanks for reading…

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

Tutorials

Categories

More articles from Infinitbility

react-native-date-picker detox example

react-native-date-picker detox example

React Native, detox guide to test react-native-date-picker example

January 11th, 2022 · 1 min read
How to show and hide password using eye icon in react native?

How to show and hide password using eye icon in react native?

React native, hide and show password when click on eye icon exmaple.

January 10th, 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]