How can I remove a character from a string using Javascript?

javascript remove specific character from string
javascript remove character from string at index
javascript remove characters from string
javascript remove first character from string
javascript remove all instances of character from string
remove last character from string javascript
javascript remove multiple characters from string
javascript string replace

I am so close to getting this, but it just isn't right. All I would like to do is remove the character r from a string. The problem is, there is more than one instance of r in the string. However, it is always the character at index 4 (so the 5th character).

example string: crt/r2002_2

What I want: crt/2002_2

This replace function removes both r

mystring.replace(/r/g, '')

Produces: ct/2002_2

I tried this function:

String.prototype.replaceAt = function (index, char) {
    return this.substr(0, index) + char + this.substr(index + char.length);
}
mystring.replaceAt(4, '')

It only works if I replace it with another character. It will not simply remove it.

Any thoughts?

var mystring = "crt/r2002_2";
mystring = mystring.replace('/r','/');

will replace /r with / using String.prototype.replace.

Alternatively you could use regex with a global flag (as suggested by Erik Reppen & Sagar Gala, below) to replace all occurrences with

mystring = mystring.replace(/\/r/g, '/');

EDIT: Since everyone's having so much fun here and user1293504 doesn't seem to be coming back any time soon to answer clarifying questions, here's a method to remove the Nth character from a string:

String.prototype.removeCharAt = function (i) {
    var tmp = this.split(''); // convert to an array
    tmp.splice(i - 1 , 1); // remove 1 element from the array (adjusting for non-zero-indexed counts)
    return tmp.join(''); // reconstruct the string
}

console.log("crt/r2002_2".removeCharAt(4));

Since user1293504 used the normal count instead of a zero-indexed count, we've got to remove 1 from the index, if you wish to use this to replicate how charAt works do not subtract 1 from the index on the 3rd line and use tmp.splice(i, 1) instead.

How can I remove a character from a string using Javascript?, How do I remove a character from a string in node JS? In Javascript, there is no remove function for string, but there is substr function. You can use the substr function once or twice to remove characters from string. You can make the following function to remove characters at start index to the end of string, just like the c# method first overload String.Remove(int startIndex):

A simple functional javascript way would be

mystring = mystring.split('/r').join('/')

simple, fast, it replace globally and no need for functions or prototypes

JavaScript String replace() Method, How do you remove a character from a string in Java? Unfortunately tere isn’t a remove String method in JavaScript, however, there is the replace method. The replace method in JavaScript takes two arguments: The characters to be replaced. The characters to replace it with. In fact, the replace method is very power, as it allows us to switch a string or character with another string or character.

There's always the string functions, if you know you're always going to remove the fourth character:

str.slice(0, 4) + str.slice(5, str.length))

How to Remove First and Last Character of String in , How do you remove all occurrences of a character from a string in JavaScript? Similarly, you can use slice to remove last character from string using JavaScript. Here is how the code looks : function remove_last_character(element) { return element.slice(0,element.length - 1) } console.log(remove_last_character('Good')) Remove Character From String Using Substr. Substr is another string manipulation method which can be used to remove character from string. It works quite similar to slice method.

Your first func is almost right. Just remove the 'g' flag which stands for 'global' (edit) and give it some context to spot the second 'r'.

Edit: didn't see it was the second 'r' before so added the '/'. Needs \/ to escape the '/' when using a regEx arg. Thanks for the upvotes but I was wrong so I'll fix and add more detail for people interested in understanding the basics of regEx better but this would work:

mystring.replace(/\/r/, '/')

Now for the excessive explanation:

When reading/writing a regEx pattern think in terms of: <a character or set of charcters> followed by <a character or set of charcters> followed by <...

In regEx <a character or set of charcters> could be one at a time:

/each char in this pattern/

So read as e, followed by a, followed by c, etc...

Or a single <a character or set of charcters> could be characters described by a character class:

/[123!y]/
//any one of these
/[^123!y]/
//anything but one of the chars following '^' (very useful/performance enhancing btw)

Or expanded on to match a quantity of characters (but still best to think of as a single element in terms of the sequential pattern):

/a{2}/
//precisely two 'a' chars - matches identically as /aa/ would

/[aA]{1,3}/
//1-3 matches of 'a' or 'A'

/[a-zA-Z]+/
//one or more matches of any letter in the alphabet upper and lower
//'-' denotes a sequence in a character class

/[0-9]*/
//0 to any number of matches of any decimal character (/\d*/ would also work)

So smoosh a bunch together:

   var rePattern = /[aA]{4,8}(Eat at Joes|Joes all you can eat)[0-5]+/g
   var joesStr = 'aaaAAAaaEat at Joes123454321 or maybe aAaAJoes all you can   eat098765';

   joesStr.match(rePattern);

   //returns ["aaaAAAaaEat at Joes123454321", "aAaAJoes all you can eat0"]
   //without the 'g' after the closing '/' it would just stop at the first   match and return:
   //["aaaAAAaaEat at Joes123454321"]

And of course I've over-elaborated but my point was simply that this:

/cat/

is a series of 3 pattern elements (a thing followed by a thing followed by a thing).

And so is this:

/[aA]{4,8}(Eat at Joes|Joes all you can eat)[0-5]+/

As wacky as regEx starts to look, it all breaks down to series of things (potentially multi-character things) following each other sequentially. Kind of a basic point but one that took me a while to get past so I've gone overboard explaining it here as I think it's one that would help the OP and others new to regEx understand what's going on. The key to reading/writing regEx is breaking it down into those pieces.

JavaScript basic: Remove a character at the specified position of a , How do I remove the first character of a string? Remove the last character To remove one character off of the end of a string, we can use the String.slice method. The beautiful thing about slice is it accepts negative numbers, so we don’t need to calculate the length like many other solutions seem to use for whatever reason.

For global replacement of '/r', this code worked for me.

mystring = mystring.replace(/\/r/g,'');

How to Remove a Character from a String in JavaScript, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Method 1 – substring function. Use the substring () function to remove the last character from a string in JavaScript. This function returns the part of the string between the start and end indexes, or to the end of the string.

How To Remove Character From String Using JavaScript, JavaScript Basic: Exercise-22 with Solution. Write a JavaScript program to remove a character at the specified position of a given string and  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.

Remove first character from a string in JavaScript, A common form of string manipulation in JavaScript is to remove a character from a string. Let's explore all of the ways we can do this in  Given a URL and the task is to remove a portion of URL after a certain character using JavaScript. split () method: This method is used to split a string into an array of substrings, and returns the new array.

Removing A Character From The Start/End of a String In Javascript, Remove First/Last Character From String Using Slice. Slice method extracts a part of the string and returns it as a new string. Let's say you want to  The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. Note: If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced. To replace all occurrences of a specified value, use the global (g

Comments
  • What's the problem here, the "r"-s were all removed weren't they?
  • The fourth character is not necessarily a "/"
  • It works great for me. While i try mystring = mystring.replace('/r','/'); it only converts the first corrector, rest of them are as it is.
  • I think it's the best solution, very simple and you don't need to know regex for it!
  • The fourth character is not necessarily a '/'
  • anyway according to tests (stackoverflow.com/questions/50463850/…) unless youre writting a high performance (10k operations per sec) javascript the performance difference is not an issue or "high price to pay" so i dont see how exactly your comment contribute to the solution
  • Aside from being inefficient (and a function like this may be used iteratively on long lists where it matters) this doesn't answer the OP's question which depends on index, not on the preceding character.
  • This does not work when removing first or last character
  • @TekTimmy Why not? str.slice(0, 0) + str.slice(1, str.length) removes the first and str.slice(0, str.length-1) + str.slice(str.length, str.length) removes the last. (Of course you would remove the redundant parts if you know you're always removing the first or last, but if they're variables they work just as well as any other index.)
  • This actually solves the OP's problem and barely gets any up votes, while the two top answers don't solve the original question!
  • This doesn't answer the question asked; it simply explains why replaceAt isn't working. It should be a comment to the original question instead of an answer. :)