Remove last four characters
var myString = "abcdefg";
var newString = myString.substr(0, myString.length-4);
// newString is now "abc"
Remove first two characters
var myString = "abcdefg";
var newString = myString.substr(2);
// newString is now "cdefg"
Notes
The substr
function can be called on any string with two integer parameters, the second optional. If only one provided, it starts at that integer and moves to the end of the string, chopping off the start. If two parameters provided, it starts at the first number and ends at the second, chopping off the start and end as it is able.
Example
abcdefghijklmnopqrstuvwxyz
The snippet on this page isn’t working for me, and is throwing the following error:
$(‘#chopper’)[0].innerText is undefined
Just a heads up!
i already try to click the button ” Press to chop” but nothing happen, how come?
Pauly (and Joshua above) – Not seeing that error, can you let me know browser/version?
Firefox Mac, Version 27.0.1 here, not working.
Chrome Mac, Version 33.0.1750.146 is working.
Safari Mac, Version 6.1.1 (7537.73.11) is working.
So it looks like you don’t Support Firefox. Wow.
Console says:
Error: TypeError: $(…)[0].innerText is undefined at css-tricks.com/snippets/javascript/trim-firstlast-characters-in-string/ on line 58
I think the code snippet for the second example has a typo:
var myString = “abcdefg”;
var newString = myString_2.substr(2);
The second line of this snippet should be:
var newString = myString.substr(2);
Thanks for the tip!
You’re right! Fixing. And burying just cause no longer relevant to thread.
Thanks !!
Chris,
Any specifics on why you chose
String.**substr**( start [, length ] )
rather thanString.**slice**( begin [, end ] )
? I did a quick search and most recommend splice as it accepts a negative value for the end point without having to calc length?slice and dice …
Just curious!
Chris, it seems FF does not support
innerText
.textContent
would be the modern equivalent: https://developer.mozilla.org/en-US/docs/Web/API/Node.textContentBut since we’re using jQuery anyway why not
$(…).text()
:)How do you do both? Meaning how do you remove the first 9 char and the last 1 char in the same string
@Wesley,
Sorry it was late last night but I’ve strung the two string modifiers mentioned below into one line:
Here’s a pen showing it in action
@Wesley what about this?
Does that do what you want?
Jon,
Thanks for this code snippet. I am more up to speed with PHP rather than Javascript and was looking around how to chop a string on both ends and this worked perfectly. TY.
Don
i tried with specific string to replace and worked. but i have a situation like this
<span class='someclass'>someclass</span>
to sometext is it possibleHow about skipping some characters and removing the character afterwards?
Like in abcdefgh how to start the trimming from d (making d the first character)
@Sarmad,
This would be the first part of the codeblock I showed above:
or
http://codepen.io/jonrandahl/pen/hulKG
Yes I got that, but I was asking about removing characters from the middle.
Like abc(removing four characters)hijklmn
@Sarmad,
Does this work for you?
http://codepen.io/jonrandahl/pen/xgDrF
Exactly. I was looking for a way to trim something out from a url within
<a>
. This is working fine now. Thanks@Sarmad,
you could also split the string on your search parameter and concat the two resulting array parts, like this:
Example…
It’s really down to how you want to find your “needle” string within your “haystack” string?
Jon
a=(123) 456-789
how to neglect the special character
insted of 9044420544 i want to diaplay *******544 how to do it in html and java sctript and after lick on button need to display 9044420544 total no
@alankar, I would copy and store the original value in a data-attribute and then mask all but the total length of the string minus 3 by using regex to replace the characters with the asterisk character. Yell if you need further help!
@alankar,
Hows this:
http://codepen.io/jonrandahl/pen/BKqPdP