ReactJS Trim String


We can use JavaScript trim method to remove spaces from both end of the string. Here in this tutorial, we are going to explain how to use trim method in reactJs. You can also use our online editor to edit and run the code online.


ReactJS Trim String Method JavaScript Example

You can use JavaScript trim() function in ReactJs Simply as below-

Example:

<script>
function MyFunction() {
  var myStr = " Hello World "; // space at both end
  var myStrLength = myStr.length;
  var myStr1 = myStr.trim();
  var myStrLength1 = myStr1.length;
  return (
    <p>Befor Trim Length = {myStrLength} | After Trim Length = {myStrLength1}</p>
  );
}

ReactDOM.render(
  <MyFunction />,
  document.getElementById('root')
);


</script>

Try it »

In the above example you can see string “Hellow World” contains blank space at the both end. If you check the length with space it will return 13, if you trim this string it will return length 11.

Output of above example-

ReactJs trim String Example


Advertisements

User Ans/Comments

This worked for me thanks!
 John

Add Comment

📖 Read More