Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first character of string?

Tags:

javascript

I have a string, and I need to get its first character.

var x = 'somestring'; alert(x[0]); //in ie7 returns undefined

How can I fix my code?

like image 412
Simon Avatar asked Aug 06 '10 19:08

Simon


People also ask

How do you get the first character of each word in a string?

To get the first letter of each word in a string: Call the split() method on the string to get an array containing the words in the string. Call the map() method to iterate over the array and return the first letter of each word. Join the array of first letters into a string, using the join() method.

How do I index the first character of a string?

To get the index of a character in a string, you use the indexOf() method, passing it the specific character as a parameter. This method returns the index of the first occurrence of the character or -1 if the character is not found.

How do you get the first character of a string in TypeScript?

TypeScript - String charAt() charAt() is a method that returns the character from the specified index. Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string, called stringName, is stringName.


1 Answers

What you want is charAt.

var x = 'some string'; alert(x.charAt(0)); // alerts 's' 
like image 199
Daniel Vandersluis Avatar answered Sep 29 '22 14:09

Daniel Vandersluis