Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
97
rated 0 times [  100] [ 3]  / answers: 1 / hits: 150216  / 13 Years ago, tue, december 13, 2011, 12:00:00

I would like to count the number of lines in a string. I tried to use this stackoverflow answer,


lines = str.split("rn|r|n"); 
return lines.length;

on this string (which was originally a buffer):


 GET / HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML,like Gecko) Chrome/15.0.874.121 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

and, for some reason, I got lines='1'.


Any idea how to make it work?


More From » string

 Answers
16

Using a regular expression you can count the number of lines as



 str.split(/rn|r|n/).length


Alternately you can try split method as below.



var lines = $(#ptest).val().split(n);  
alert(lines.length);


working solution: http://jsfiddle.net/C8CaX/


[#88582] Monday, December 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryonk

Total Points: 161
Total Questions: 116
Total Answers: 107

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
bryonk questions
;