Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first letter from string using jquery [duplicate]

Tags:

jquery

I am poor knowledge in jquery. I have mentioned the script below

var header = $('.time'+col).text(); alert(header); 

I got string as "109:00AM" from that how to get first letter such as 1. Could you please help me.

like image 228
user2310209 Avatar asked Sep 19 '13 06:09

user2310209


People also ask

How do I get the first letter of a string in jQuery?

“jquery string get first character” Code Answer'svar str="Hello Folks!"

How to get the first letter in a string js?

Get the First Letter of the String You should use the charAt() method, at index 0, to select the first character of the string. NOTE: charAt is preferable than using [ ] (bracket notation) as str. charAt(0) returns an empty string ( '' ) for str = '' instead of undefined in case of ''[0] .

How do I get the first letter of a string?

To get the first and last characters of a string, access the string at the first and last indexes. For example, str[0] returns the first character, whereas str[str. length - 1] returns the last character of the string.

How do I get the first character of a textbox in jQuery?

You can use charAt to get the first letter. var x = 'some text'; alert(x. charAt(0)); If you're using jQuery and have a textbox with id="firstName" you can access the first letter in the following way.


1 Answers

Try with charAt(0) like

var header = $('.time'+col).text(); alert(header.charAt(0)); 

You can also use substring like

alert(header.substring(1)); 

And as @Jai said you can use slice also like

alert(header.slice(0,1)); 
like image 61
Gautam3164 Avatar answered Sep 30 '22 17:09

Gautam3164