I need to grab first word in string and I need to convert it to integer.
How to do this using jQuery?
example : “223 Lorem Ipsum Dolor”
I need “223” and it must be converted into integer…
Any help would be appreciated.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can split a string based on any character (like a space), then pass the first index to
parseIntDEMO
Note that
parseInttakes a second parameter, which is the radix. If you leave that off, and try to parse a number with a leading zero, like09, it’ll assume you’re in base 8, and will return 0, since09isn’t a valid base-8 value.Or, as John points out, using the unary
+operator is a nifty way to convert a string to a number:DEMO