6

How to remove white spaces from a string in jQuery

 1 year ago
source link: https://www.laravelcode.com/post/how-to-remove-white-spaces-from-a-string-in-jquery
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to remove white spaces from a string in jQuery

  9539 views

  2 years ago

jQuery

Use the jQuery $.trim() function

You can use the jQuery $.trim() function to remove all the spaces (including non-breaking spaces), newlines, and tabs from the beginning and end of the specified string.

However, the whitespaces in the middle of the string are preserved. The following example will show you how to remove leading and trailing whitespaces from a string of text.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Remove White Space from Strings</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function(){
        var myStr = $(".original").text();
        var trimStr = $.trim(myStr);
        $(".trimmed").html(trimStr);
    });
</script>
</head>
<body>
    <h3>Original String</h3>
    <pre class="original">      Paragraph of text with       multiple    white   spaces before and after.       </pre>
    <br>
    <h3>Trimmed String</h3>
    <pre class="trimmed"></pre>
</body>
</html>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK