The index of the first character in a string is 0 while the index of the last character is length

string index string charIndex

Returns the charIndex 'th character of the string argument. A charIndex of 0 corresponds to the first character of the string. charIndex may be specified as follows:

integer — The char specified at this integral index.end — The last char of the string.end-integer — The last char of the string minus the specified integer offset (e.g. end-1 would refer to the "c" in "abcd").

If charIndex is less than 0 or greater than or equal to the length of the string then an empty string is returned.


AMG: TIP #176 [L1 ] adds two more more indexing modes: integer-integer and integer+integer. These are certainly convenient. However, due to the fundamental need for concatenation and reparsing in any practical application, they are also slightly slower than just using [expr] to do the math. (Remember to always brace your expr-essions!)

There's also another mode which may be occasionally useful: end+integer. This comes in handy when the integer in question is already negative.


Returns for index an integer, the charIndex'th character in string. If index is end, the last character in string, and if index is end-integer, the last character minus the specified offset. (From the Tcl/Tk Reference Guide)


For example,

   string index abcde 3

returns "d" . Notice that the index is zero based - the first character is index 0.

   string index abcde 10

returns "" . There is no 10th character. No error is returned here.


See also

  • string
  • string range

Very readable code is to use .substring() with a start set to index of the second character (1) (first character has index 0). Second parameter of the .substring() method is actually optional, so you don't even need to call .length()...

TL;DR : Remove first character from the string:

str = str.substring(1);

...yes it is that simple...

Removing some particular character(s):

As @Shaded suggested, just loop this while first character of your string is the "unwanted" character...

var yourString = "0000test";
var unwantedCharacter = "0";
//there is really no need for === check, since we use String's charAt()
while( yourString.charAt(0) == unwantedCharacter ) yourString = yourString.substring(1);
//yourString now contains "test"

.slice() vs .substring() vs .substr()

EDIT: substr() is not standardized and should not be used for new JS codes, you may be inclined to use it because of the naming similarity with other languages, e.g. PHP, but even in PHP you should probably use mb_substr() to be safe in modern world :)

Quote from (and more on that in) What is the difference between String.slice and String.substring?

He also points out that if the parameters to slice are negative, they reference the string from the end. Substring and substr doesn´t.

Examples

Get the first character in a string:

let text = "HELLO WORLD";
let letter = text.charAt(0);

Try it Yourself »

Get the second character in a string:

let text = "HELLO WORLD";
let letter = text.charAt(1);

Try it Yourself »

Get the last character in a string:

let text = "HELLO WORLD";
let letter = text.charAt(text.length-1);

Try it Yourself »

More examples below.


Definition and Usage

The charAt() method returns the character at a specified index (position) in a string.

The index of the first character is 0, the second 1, ...


Syntax

Parameters

Parameter Description
index Optional.
The index (position) of the character.
Default = 0.

Return Value

Type Description
String The character at the specified index.
Empty string ("") if the index is out of range.


More Examples

Index out of range returns empty string:

let text = "HELLO WORLD";
let letter = text.charAt(15);

Try it Yourself »

Default index is 0:

let text = "HELLO WORLD";
let letter = text.charAt();

Try it Yourself »

Invalid index converts to 0:

let text = "HELLO WORLD";
let letter = text.charAt(3.14);

Try it Yourself »


Browser Support

charAt() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes


What is the index of the first character in a string variable?

Strings are zero-indexed: The index of a string's first character is 0 , and the index of a string's last character is the length of the string minus 1.

How do you find the last index of a string?

The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string. The lastIndexOf() method searches the string from the end to the beginning. The lastIndexOf() method returns the index from the beginning (position 0).

How do you check if the first character of a string is a specific character?

Using the isDigit() method Therefore, to determine whether the first character of the given String is a digit. The charAt() method of the String class accepts an integer value representing the index and returns the character at the specified index.

How do I find the index of a character in a string?

The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.