How To Remove Character From String Using JavaScript

In this tutorial, you’ll be going to learn how to remove character from string using javascript. The easiest approach to use slice(), substr(), substring() and replace().

Remove character from string is a common developer task which you also encounter during software development. Sometimes you’ll also face the interview question related to this post title.

There are multiple ways you’ll going to remove a character from string in javascript. So do not miss any part of this post and stick to end. Who knows which method works best for you.

Also, see our other javascript series-

  1. How to Reverse a String in JavaScript
  2. Find Max and Min Value from an Array in JavaScript
  3. How To Empty An Array In JavaScript
  4. Check if the object is empty in javascript
  5. How to check if array is empty in javascript
How To Remove Character From String Using JavaScript
JavaScript Remove Character From String

How To Remove Character From String Using JavaScript

  • Table Of Content
    • Javascript Remove First Character From String Using splice()
    • JavaScript Remove Last Character From String Using splice()
    • Remove First and Last Character From String Together
    • JavaScript Remove Character From String Using substr()
    • Remove Character From String Using substring()
    • Using replace() Remove substring from string
      • Remove all occurrence of a substring using replace
    • split and join to remove substring from string

We will be going to discuss all the above point in detail.

Javascript Remove First Character From String Using slice()

So we will be going to remove the first character from string using splice() method. Array.prototype.slice() can be used with array and string. Slice method extracts a substring from the main javascript string and returns a new string.

Syntax: string.slice(startIndex, lastIndex)

startIndex: Start index specifies the starting the index of a string but it’s optional

lastIndex: Last index specify the index which is more than starting index but less than the length of the string. But it’s optional

Now, look at an example.


let string = "Hello"

let remove_first_charater = string.slice(1)

console.log(remove_first_charater)

// result => ello

In the above example, from "Hello" string, we remove the first character and it becomes "ello". We can also optimize the above code and make it reusable.

function remove_first_character(element) {
  return element.slice(1)
}

console.log(remove_first_character('Hello'))
// result => ello

console.log(remove_first_character('String'))
// result => tring

JavaScript Remove Last Character From String Using Splice()

Now we remove last character from a string in javscript using splice() method. Splice is a very nice method which also works with array also.

Now let’s see remove last character from string example.


function remove_last_character(string) {
   return string.slice(0,string.length - 1)
}

console.log(remove_last_character('Hello'))
// result => Hell

console.log(remove_last_character('String'))
// result => Strin

So as you can see using splice() method. we can remove first character and last character from string. But what about if we want to remove first and last character together.

Remove First and Last Character From String Together

We already have seen how to remove the first character from the string and how to remove the last character from string but independently. Now we need to remove first and last charater from string together.

For this, we need to use chaining of slice() a method with another slice() method. Let’s look at an example.



function remove_character(string) {
   return string.slice(0,string.length - 1).slice(1)
}

console.log(remove_character('Hello'))
// result => ell

console.log(remove_character('String'))
// result => trin

So we remove the start and end character from string together. But remember this point, remove the last character from string first then the first character. Why check yourself and let me know in the comment.

JavaScript Remove Character From String Using substr()

substr() also another good aproach to remove character from string in javascript. It mostly works the same as splice() method.

Now move to our code section.



function remove_first_character(string) {
   return string.substr(1)
}

function remove_last_character(string) {
   return string.substr(0,string.length - 1)
}

console.log(remove_first_character('Hello'))
// result => ell

console.log(remove_last_character('Hello'))
// result => trin

As splice and substr both methods works in the same but you can also find a difference between them on the internet.

JavaScript Remove Character From String Using substring()

substring method is like substr which also remove a character from a string. Now let’s see it’s example.


function remove_first_character(string) {
   return string.substring(1)
}

function remove_last_character(string) {
   return string.substring(0,string.length - 1)
}

console.log(remove_first_character('Hello'))
// result => ell

console.log(remove_last_character('Hello'))
// result => trin

Until now we only remove first and last character from string. But if want to remove series of given string then using the above method we can not achieve that.

We have to use another approach to tackle this situation.

Using replace() Remove SubString From String

For this approach, we need to use two things replace and regex. Because with the help of regex we find the occurrence of a string and using the replace method we replace with an empty string.

Now move on to our code section.

function remove_character(remove_string, main_string) {
  let reg_exp = new RegExp(remove_string)
  return main_string.replace(reg_exp, '')
}

console.log(remove_character('hello', 'hellocodekila'))
// result => codekila

But there is an issue with the above method. Replace method only replace the first occurrence of the string. But using a proper regex we can replace all occurrence of string.

Remove all occurrence of string using replace

let string = "hellocodekilahello"
let remove_substring = string.replace(/hello/g, '');

console.log(remove_substring)
// result => codekila

So to achieve the above issue we have another method split and join which we can use.

split and join to remove substring from a string

Using a split method, we break the string into an array but to break the string. We use the string which we need to remove and then we join again using Array.join() method.

function remove_character(remove_string, main_string) {
  return main_string.split(remove_string).join('')
}

console.log(remove_character('hello', 'hellocodekilahello'))
// result => codekila

So it’s also an easy approach which you can use to remove particular substring from a string.

Conclusion :

As you can see there are many ways you can implement to how to remove characters from a string in javascript

We have used only functions and functionality provide by JavaScript only like an slice,substr, substring and more.

And we have also implemented other approaches to remove multiple characters from string based on condition and requirement.

I hope you read the full article and I am very excited to get a comment on this post to get your review.

If you have any question please feel free to ask me in the comment section. And let me know if you have any suggestion is also acceptable. Also, like our Facebook Page.

You can also check :

  1. Phone Number Validation in HTML5
  2. Convert PHP array to JavaScript array
  3. Get Multiple Elements By Id Using Javascript
  4. Remove duplicates from a javascript array

Leave a Reply

Your email address will not be published. Required fields are marked *