47

How can I remove the first three letters of a string in JQuery?

For example: Turn cat1234 to 1234

Jacob
  • 6,317
  • 10
  • 40
  • 58

11 Answers11

106

No jQuery needed.

"cat1234".slice(3);

or

"cat1234".substring(3);

or

"cat1234".substr(3);

var str="cat1234";
console.log("using slice =",str.slice(3));
console.log("using substring =",str.substring(3));
console.log("using substr =",str.substr(3));
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
jAndy
  • 231,737
  • 57
  • 305
  • 359
9
var val = 'cat1234';
var newVal = val.substring(3, val.length);
Martin.
  • 10,494
  • 3
  • 42
  • 68
Jwalin Shah
  • 2,451
  • 1
  • 17
  • 22
5

You don't need jQuery to do this, JavaScript will do:

"cat1235".substring(3) // yields 1235
Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
4

Use javascript's substr

demo

var str = "cat123";
alert(str.substr(3));
Martin.
  • 10,494
  • 3
  • 42
  • 68
4

How about vanilla javascript:

'cat1234'.slice(3)
# returns '1234'
WTK
  • 16,583
  • 6
  • 35
  • 45
3

You don't have to use jquery to do that, use simple javascript:

var txt = 'cat1234';
var txt2 = txt.substr(3);
Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
3
function trimCat() { return "cat1234".substring(3, 6); }

or

function trimAnotherCat() { return "cat1234".replace("cat", ""); }
jenson-button-event
  • 18,101
  • 11
  • 89
  • 155
  • seriously, where has all the love gone? – jenson-button-event Nov 25 '11 at 11:03
  • The snippet you posted is wrong and can be used only in functions. alert is correct one.. – Martin. Nov 25 '11 at 11:04
  • 1
    @BobTodd: Balance restored, this is a more than perfectly valid answer :). @Martin: `alert()` is no more valid than `return` in this context...OP has not actually specified what they want – Clive Nov 25 '11 at 11:09
  • @Martin: What if the OP doesn't want an alert box? You're just guessing, which is no more valid than BobTodd's guess of using `return` from a function. End of story. – Clive Nov 25 '11 at 11:11
  • @Clive ah, arbitration. i now feel all warm and valid :] – jenson-button-event Nov 25 '11 at 11:11
  • @Bob okay, if you edit your answer, (it's "locked" now), I can remove my downvote. – Martin. Nov 25 '11 at 11:12
  • 1
    I'd quibble with the second formulation; you need to know the first three letters were "cat." I think you were just trying to live up to the aphorism that there's more than one way to trim a cat. – Michael Blackburn Mar 24 '17 at 14:12
3
var cat1234 = 'cat1234';
var new1234 = cat1234.substring(3);
Martin.
  • 10,494
  • 3
  • 42
  • 68
martincarlin87
  • 10,848
  • 24
  • 98
  • 145
2

Just plain JavaScript:

var s = 'cat1234';
console.log(s.substr(3));

All String methods can be found here.

bjornd
  • 22,397
  • 4
  • 57
  • 73
0

JavaScript can do the trick.

var s = 'cat1234';

console.log(s.toString().slice(3));

This will output: 1234

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
0
var str = 'cat1234';
var newStr = str.slice(3);

it works for me. slice(3) will slice first 3 character from string.