{"id":20177,"date":"2022-05-25T16:55:19","date_gmt":"2022-05-25T11:25:19","guid":{"rendered":"https:\/\/java2blog.com\/?p=20177"},"modified":"2022-05-25T16:55:19","modified_gmt":"2022-05-25T11:25:19","slug":"remove-first-character-from-string-javascript","status":"publish","type":"post","link":"https:\/\/java2blog.com\/remove-first-character-from-string-javascript\/","title":{"rendered":"Remove First Character from String in JavaScript"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Ways_to_Remove_First_Character_from_string_in_JavaScript\">Ways to Remove First Character from string in JavaScript<\/a><ul><li><a href=\"#Using_substring\">Using substring()<\/a><\/li><li><a href=\"#Using_slice\">Using slice()<\/a><\/li><li><a href=\"#Using_substr\">Using substr()<\/a><\/li><li><a href=\"#Using_replace_method\">Using replace() method<\/a><\/li><li><a href=\"#Using_Array8217s_shift_with_split_and_join\">Using Array&#8217;s shift() with split() and join()<\/a><\/li><\/ul><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n<p>In this post, we will see how to remove first character from string in JavaScript.<\/p>\n<p>Since Strings are immutable in JavaScript, you can&#8217;t do it in-place. All the methods will return new String and you can either assign it to same variable or to new one.<\/p>\n<h2><span id=\"Ways_to_Remove_First_Character_from_string_in_JavaScript\">Ways to Remove First Character from string in JavaScript<\/span><\/h2>\n<p>There are multiple ways to remove first character from String in JavaScript. Let&#8217;s go through them.<\/p>\n<h3><span id=\"Using_substring\">Using substring()<\/span><\/h3>\n<p><strong>To remove first character from string in JavaScript, call <code>substring()<\/code> method on string with start index as 1.<\/strong><\/p>\n<p>Here is simple example:<\/p>\n<pre code=\"javascript\" mark=\"3\" title=\"Using substring() method\">\nvar str='Java2blog';\nstr = str.substring(1);\nconsole.log(str);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre code=\"javascript\" title=\"Output\">\nava2blog\n<\/pre>\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/substring\" title=\"string&#039;s substring()\" target=\"_blank\" rel=\"noopener\">string&#8217;s substring()<\/a> method returns part of the String between start and end indexes or to the end of String if end index is not provided.<\/p>\n<p>If you want to put some condition on first character, then you can use <code>charAt()<\/code> with <code>substring()<\/code><\/p>\n<pre code=\"javascript\" mark=\"3\" title=\"Using substring() method with charAt()\">\nvar str='Java2blog';\n\n\/\/ Remove first character only if it is equal to 'J'\nif(str.charAt(0) ==='J')\n    str = str.substring(1);\nconsole.log(str);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre code=\"javascript\" title=\"Output\">\nava2blog\n<\/pre>\n<p>You can use this solution to remove first n characters from String.<\/p>\n<p><strong>For example:<\/strong><br \/>\nLet&#8217;s say you want to remove first 3 characters of the String. You can pass start index as 3 and <code>substring()<\/code> will return remaining string after first 3 characters.<\/p>\n<pre code=\"javascript\" mark=\"3\" title=\"Using substring() method\">\nvar str='Java2blog';\nstr = str.substring(3);\nconsole.log(str);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre code=\"javascript\" title=\"Output\">\na2blog\n<\/pre>\n<h3><span id=\"Using_slice\">Using slice()<\/span><\/h3>\n<p>To remove first character from string in JavaScript, call <code>slice()<\/code> method on string with start index as 1.<\/p>\n<p>Here is simple example:<\/p>\n<pre code=\"javascript\" mark=\"3\" title=\"Using slice() method\">\nvar str='Java2blog';\nstr = str.slice(1);\nconsole.log(str);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre code=\"javascript\" title=\"Output\">\nava2blog\n<\/pre>\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/slice\" title=\"string&#039;s slice()\" target=\"_blank\" rel=\"noopener\">string&#8217;s slice()<\/a> method extracts part of the String and returns new string without changing original string.<\/p>\n<p>You can use this solution to remove first n characters from String.<\/p>\n<p><strong>For example:<\/strong><br \/>\nLet&#8217;s say you want to remove first 3 characters of the String. You can pass start index as 3 and <code>slice()<\/code> will return remaining string after first 3 characters.<\/p>\n<pre code=\"javascript\" mark=\"3\" title=\"Using slice() method\">\nvar str='Java2blog';\nstr = str.slice(3);\nconsole.log(str);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre code=\"javascript\" title=\"Output\">\na2blog\n<\/pre>\n<h3><span id=\"Using_substr\">Using substr()<\/span><\/h3>\n<p>To remove first character from string in JavaScript, call <code>substr()<\/code> method on string with start index as 1.<\/p>\n<blockquote>\n<p><code>substr()<\/code> is deprecated method now and should be avoided.<\/p>\n<\/blockquote>\n<p>Here is simple example:<\/p>\n<pre code=\"javascript\" mark=\"3\" title=\"Using substr() method\">\nvar str='Java2blog';\nstr = str.substr(1);\nconsole.log(str);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre code=\"javascript\" title=\"Output\">\nava2blog\n<\/pre>\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/substr\" title=\"string&#039;s substr()\" target=\"_blank\" rel=\"noopener\">string&#8217;s substr()<\/a> method returns portion of the String and extending for given number of characters after that index. If length is not defined, then it will return portion of string upto the end.<\/p>\n<p>You can use this solution to remove first n characters from String.<\/p>\n<p><strong>For example:<\/strong><br \/>\nLet&#8217;s say you want to remove first 3 characters of the String. You can pass start index as 3 and <code>substr()<\/code> will return remaining string after first 3 characters.<\/p>\n<pre code=\"javascript\" mark=\"3\" title=\"Using substr() method\">\nvar str='Java2blog';\nstr = str.substr(3);\nconsole.log(str);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre code=\"javascript\" title=\"Output\">\na2blog\n<\/pre>\n<h3><span id=\"Using_replace_method\">Using replace() method<\/span><\/h3>\n<p>To remove first character from string in JavaScript, get first character of String and replace it with empty String.<\/p>\n<pre code=\"javascript\" mark=\"3,4\" title=\"Using replace() method\">\nvar str='Java2blog';\nconst firstChar =  str.charAt(0);\nstr =  str.replace(firstChar,'');\nconsole.log(str);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre code=\"javascript\" title=\"Output\">\na2blog\n<\/pre>\n<blockquote>\n<p>Please note that <code>replace()<\/code> method is only going to replace first occurence of the character and that&#8217;s what we require here.<\/p>\n<\/blockquote>\n<h3><span id=\"Using_Array8217s_shift_with_split_and_join\">Using Array&#8217;s shift() with split() and join()<\/span><\/h3>\n<p>To remove first character from string in JavaScript, convert string to array using <code>split()<\/code> method, call shift on array and convert back to string using <code>join()<\/code> method.<\/p>\n<pre code=\"javascript\" title=\"Using Array's shift() with split() and join()() method\">\nvar str='Java2blog';\n\n\/\/ Convert string to array\nconst charArr =  str.split('');\n\n\/\/ Call shift on array\ncharArr.shift();\n\n\/\/ Join array back using join() method\nstr= charArr.join('');\nconsole.log(str);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre code=\"javascript\" title=\"Output\">\na2blog\n<\/pre>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>To remove first character from string in JavaScript, use <code>substring()<\/code>, <code>slice()<\/code> and <code>substr()<\/code>.<\/p>\n<p>Since <code>substr()<\/code> is deprecated now, it is not recommended solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsWays to Remove First Character from string in JavaScriptUsing substring()Using slice()Using substr()Using replace() methodUsing Array&#8217;s shift() with split() and join()Conclusion In this post, we will see how to remove first character from string in JavaScript. Since Strings are immutable in JavaScript, you can&#8217;t do it in-place. All the methods will return new String [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[42],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20177"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=20177"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20177\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=20177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=20177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=20177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}