Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»How to Remove Spaces from String in JavaScript

    How to Remove Spaces from String in JavaScript

    RahulBy RahulDecember 3, 20142 Mins ReadUpdated:February 26, 2021

    Question – How to remove extra spaces from a string using JavaScript?

    A space (” “) character is used to separate two words. Some times we get the extra spaces in a string, which may case issues with your application.

    JavaScript trim() function is used for removing leading (before) and trailing (after) spaces from a string. This tutorial will help you to remove extra spaces from a string using JavaScript function.

    Remove Spaces with JavaScript trim() Function

    For the example, assign a string with spaces to a variable. Then use JavaScript trim() function to remove leading and trailing spaces from the string.

    JavaScript
    1
    2
    3
    4
    5
    <script>
      str="   Welcome to TecAdmin.net!   ";
      newStr = str.trim();
      console.log(newStr);
    </script>

    Output:

    Welcome to TecAdmin.net!
    

    JavaScript Remove Spaces Anywhere in String

    You can remove all spaces from a string with combination of JavaScript split() and join() functions. Here we will split the string with space characters as delimiter. The again join the all parts of string without any space.

    JavaScript
    1
    2
    3
    4
    5
    6
    <script>
      str="Welcome to TecAdmin.net ";
      str1 = str.split(" ");
      newStr = str1.join("");
      console.log(newStr);
    </script>

    You can also run both functions in single line.

    JavaScript
    1
    2
    3
    4
    5
    <script>
      str="Welcome to TecAdmin.net ";
      newStr = str.split(" ").join("");
      console.log(newStr);
    </script>

    Output:

    WelcometoTecAdmin.net
    

    Using Left or Right Trim in JavaScript

    Some of the older browsers may not support JavaScript trim() function. In that case, we can use the replace() method to remove trailing and preceding white spaces from any string.

    • Trim Space Both Side:
      JavaScript
      1
      alltrimStr = str.replace(/^s+|s+$/gm,'');
    • Trim Spaces Left Side:
      JavaScript
      1
      ltrimStr = str.replace(/^s+/,'');
    • Trim Spaces Right Side:
      JavaScript
      1
      rtrimStr = str.replace(/s+$/,'');

    Conclusion

    This tutorial described you to remove extra spaces from a string using JavaScript. Additionally, provides you instructions to remove all spaces from a string.

    javascript js string trim
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Disable root Login in phpMyAdmin
    Next Article How to Transfer files securely using SCP Command in Linux

    Related Posts

    JavaScript Program to Add Two Numbers

    2 Mins Read

    Shell Script – Remove Double Quote (“”) from a String

    1 Min Read

    How to Install ReactJS on Ubuntu 20.04

    3 Mins Read

    Remove First Character from String in JavaScript

    1 Min Read

    How To Convert String to Integer in Python

    Updated:December 15, 20212 Mins Read

    10 JavaScript Console Methods

    Updated:November 14, 20203 Mins Read

    1 Comment

    1. sachin on June 19, 2016 5:08 pm

      Refer this example to trim a string using JavaScript.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    • (Resolved) Please install all available updates for your release before upgrading
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.