{"id":3856,"date":"2013-05-17T19:05:17","date_gmt":"2013-05-17T19:05:17","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3856"},"modified":"2013-05-17T19:05:17","modified_gmt":"2013-05-17T19:05:17","slug":"javascript-remove-space-between-string","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/javascript-remove-space-between-string\/","title":{"rendered":"JavaScript: Remove Space From String Examples"},"content":{"rendered":"<p>Do you wish to remove space from string using JavaScript? In this article, I am going to share examples on how you can <em><strong>remove space between string<\/strong><\/em> very easily. Read on to find out more.<\/p>\n<p><!--more--><\/p>\n<h2>How To Remove Space From String Using JavaScript<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3856\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s assume that we have a string with the word &#8220;S t r i n g&#8221;. Note that each letter of the word is\u00a0separated\u00a0by a space. Now we will remove the space between each of these letters.<\/p>\n<h3>Example 1: Remove all spaces completely<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\nvar original_str = \"S t r i n g\"\n\n\/\/Strips all spaces\nvar space_stripped_str = original_str.replace(\/\\s\/g,'');\n\nalert(space_stripped_str + ' &lt;---- Without spaces');\n\n&lt;\/script&gt;<\/pre>\n<p>When you run the above code, you will see an alert that gives you the\u00a0following\u00a0output: &#8220;String&#8221;<\/p>\n<h3>Example 2: Remove multiple spaces and replace them with single space<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\nvar original_str2 = \"S    t    r  in  g\"\n\/\/Strips excessive white spaces i.e. retains only one space between each letter\nvar white_space_stripped_str = original_str2.replace(\/\\s+\/g, ' ');\n\nalert(white_space_stripped_str + ' &lt;---- With exactly one space between each letter in case each letter has multiple spaces');\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 3: Remove spaces completely (trim) from left of the string<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\nvar original_str3 = \"   This is a string\"\n\n\/\/Strips all space to the left of the string\nalert( original_str3.trimLeft() + ' &lt;--- Without any spaces on the left' );\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 4: Remove\u00a0spaces completely (trim) from right of the string<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\nvar original_str4 = \"This is a string   \"\n\n\/\/Strips all space to the right of the string\nalert( original_str4.trimRight() + ' &lt;--- Without any spaces on the right' );\n\n&lt;\/script&gt;<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3856\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>Isn&#8217;t that easy? Your Turn Now!<\/h5>\n<p>Do you know of any other ways to <em><strong>remove spaces using JavaScript from a word or a string<\/strong><\/em>? Please feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you wish to remove space from string using JavaScript? In this article, I am going to share examples on how you can remove space between string very easily. Read on to find out more.<\/p>\n","protected":false},"author":1,"featured_media":352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17,13],"tags":[35],"class_list":["post-3856","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-tips-and-tricks","category-tips-and-tricks","tag-javascript-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-10c","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=3856"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3856\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=3856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}