4

Possible Duplicate:
javascript substring

How can I do substr (php) in JS?

Example code from PHP:

<?php
$rest = substr("abcdef", -1);    // returns "f"
$rest = substr("abcdef", -2);    // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
?>
Community
  • 1
  • 1
Dudu
  • 73
  • 7

2 Answers2

7

It is very similar to PHP:

var rest = "abcdef".substr(start, length);

You also can read more about this function here: http://www.w3schools.com/jsref/jsref_substr.asp

user1123379
  • 334
  • 3
  • 5
  • 13
2

The following will reproduce it.

"abcdef".substr(-1);
"abcdef".substr(-2);
"abcdef".substr(-3, 1);
Martin.
  • 10,494
  • 3
  • 42
  • 68
  • 1
    I don't think IE supports negative starting positions with `.substr()`, though it does with `.slice()`... – nnnnnn Feb 20 '12 at 20:25