Finding A Character's Position In A String Using charAt()





string.charAt(integer)

Each character in a string is assigned an index position. Strings, like arrays, begin counting characters at position zero.

The charAt() method will return the value of the index position that you declare as its parameter. The parameter can be an integer or any expression representing an integer.

Like arrays, strings have a length property. And just like arrays, the length will always be one more than the index position of the last character. So to extract the value of the last character without knowing how many characters exist, use the following expression in place of an integer:
charAt(document.formName.formElementName.value.length-1)

Use this method only when you want to extract the value of a single character.

View the Source


 


View Source | Home | Contact