How to Remove all Spaces from String in React

Published on Nov 19, 2020

Hi artisans, sometimes we need to remove all spaces from string. In this artcle, we’re going to remove all spaces from string in React.

Remove Spaces

import React from "react";

function App() {

  var myStr = " Wel come To My Note Paper ";
  var newStr = myStr.replace(/\s/g, "");

  return(
    <div>
      <p><strong>String: </strong>{myStr}</p>
      <p><strong>Without Spaces: </strong>{newStr}</p>
    </div>
  )
}

export default App;

Output

String: Wel come To My Note Paper

Without Spaces: WelcomeToMyNotePaper
That’s it. Thanks for reading. ?

Comments (0)