Show of hands if you can off of the top of your head remove/trim a character from the start or end of a string. If your hand remained down, don’t feel too bad. There are so many string manipulation methods in Javascript, it can be confusing.
We have; substr
, substring
and splice
(just off the top of my head). Not many people can tell you the differences between the three without Googling.
In this weeks lesson of, “You probably already know this, but some people might not”, we are going to be removing characters from the beginning and end of strings. Riveting stuff, right?
You didn’t come here for a lesson, so let’s just jump right into some code, shall we? The only two methods you need to know are substr
and slice
the rest, not so much. Although, always make sure you test any code that you just find on a random blog like this.
Remove the first character
Want to remove the first character from a string? Just bump the number up if you want to remove more than one character from the beginning of the string.
var myString = '!thisIsMyString'; var sillyString = myString.substr(1);
If you wanted to remove say 5 characters from the start, you’d just write substr(5)
and you would be left with IsMyString
using the above example.
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. You don’t need to calculate the string length to remove a character and or characters off of the end.
var myString = 'thisIsMyString!'; var sillyString = myString.slice(0, -1);
Remove the first character and last character
What if you wanted to remove a character from the beginning of a string as well as from the end at the same time? Because of the way string methods work, they are chainable.
var myString = '!thisIsMyString!'; var sillyString = myString.substr(1).slice(0, -1);
A shorter way is myString.slice(1, -1). This gives both the start and end index to the slice call.
sillyString =myString .substring(1,myString .length – 1);
depends on the needs you can pass first and last argument here pass 1 for remove first and last
What if i want to remove first [ and last ] from the array string which has [ in start and last value gets closed ]. I have tried multiple options to replace them with space but its not working
For remove the first character and last character you can also do this : `var sillyString = myString.slice(1, -1)`
How can I remove all?
Example :
var mystr = ‘my <name is <Mr Baby<<';
How can i remove all of '<' symbols ?
@EL MOT
`mystr.replace(“<", "")` will work
In the entire article, `substr` (which I’ve seen in some places as “not recommended” and “pseudo-deprecated”) can be replace with “slice”
substr is deprecated and the best replacement is substring
Or slice, depending on your needs. I personally use slice a lot.
P.S. it’s 2019! Found this when searching for “CerebralDatabank” lol
I’m wondering how one would remove all characters after a specific character. For instance, I have a series of strings:
“WhatDoIWantToEat~Everything”
“WhatDoIWantToWrite~Everything”
“WhatDoYouWantToSing~Everything”
I just want to return “Everything” so I’d like to remove all characters after the ~, including the ~. What would this look like?
@Steve
You may wanna use String.indexOf(searchedChars) to get the first occurence of a substring.
tnq :X
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.