• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Removing A Character From The Start/End of a String In Javascript

Javascript · May 5, 2016

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.

why-dont-we-have-both
var myString = '!thisIsMyString!';
var sillyString = myString.substr(1).slice(0, -1);

Dwayne

Leave a Reply Cancel reply

12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
RLa
RLa
6 years ago

A shorter way is myString.slice(1, -1). This gives both the start and end index to the slice call.

5
Pawan Kuar Tiwari
Pawan Kuar Tiwari
5 years ago

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

0
rahul
rahul
5 years ago

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

0
Julien Rabin
Julien Rabin
4 years ago

For remove the first character and last character you can also do this : `var sillyString = myString.slice(1, -1)`

0
EL MOT
EL MOT
4 years ago

How can I remove all?
Example :
var mystr = ‘my <name is <Mr Baby<<';
How can i remove all of '<' symbols ?

0
Saelorable
Saelorable
4 years ago

@EL MOT
`mystr.replace(“<", "")` will work

0
CerebralDatabank
CerebralDatabank
4 years ago

In the entire article, `substr` (which I’ve seen in some places as “not recommended” and “pseudo-deprecated”) can be replace with “slice”

0
kats Isaac
kats Isaac
4 years ago

substr is deprecated and the best replacement is substring

1
CerebralDatabank
CerebralDatabank
4 years ago

Or slice, depending on your needs. I personally use slice a lot.
P.S. it’s 2019! Found this when searching for “CerebralDatabank” lol

0
Steve
Steve
3 years ago

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?

0
IC
IC
3 years ago

@Steve
You may wanna use String.indexOf(searchedChars) to get the first occurence of a substring.

0
usEr
usEr
3 years ago

tnq :X

0

Primary Sidebar

Popular

  • Thoughts on the Flipper Zero
  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • How To Paginate An Array In Javascript
  • Waiting for an Element to Exist With JavaScript
  • Reliably waiting for network responses in Playwright
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • Removing A Character From The Start/End of a String In Javascript

Recent Comments

  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die
  • Dwayne on How to Create a Blockchain With TypeScript
  • kevmeister68 on PHP Will Not Die

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz