Removing a specific substring from a string in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

<p>We are given a main string and a substring, our job is to create a function, let&rsquo;s say removeString() that takes in these two arguments and returns a version of the main string which is free of the substring.</p><p>Here, we need to remove the separator from a string, for example &minus;</p><pre class="result notranslate">this-is-a-sting</pre><p>Let&rsquo;s now write the code for this function &minus;</p><pre class="prettyprint notranslate">const removeString = (string, separator) =&gt; { &nbsp; &nbsp;//we split the string and make it free of separator &nbsp; &nbsp;const separatedArray = string.split(separator); &nbsp; &nbsp;//we join the separatedArray with empty string &nbsp; &nbsp;const separatedString = separatedArray.join(&quot;&quot;); &nbsp; &nbsp;return separatedString; } const str = removeString(&#39;this-is-a-sting&#39;, &#39;-&#39;); console.log(str);</pre><p>The output of this code in the console will be &minus;</p><pre class="result notranslate">thisisastring</pre>
raja
Updated on 09-Oct-2020 11:51:00

Advertisements