Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: A better way of finding characters than looping?


Message #1 by "Boloian, John" <jboloian@u...> on Mon, 6 May 2002 10:27:27 -0400
Thanks Chris! 

I'm going to test that for speed vs. another method I came up with: using
split() to split the value into an array along the '\n' and using the length
of that array...

Regards,

John Boloian

p.s. Off subject: I thought this was pretty neat (check out the timeline):

http://www.google.com/googlegroups/archive_announce_20.html



-----Original Message-----
From: Chris Scott [mailto:chris@e...]
Sent: Tuesday, May 07, 2002 7:56 AM
To: javascript
Subject: [javascript] Re: A better way of finding characters than
looping?


Hi John,

You could use a regular expression to return a match array for new line
characters, then check its length...

function myFunc(){
 var regEx=/\n/g;
 var value=document.forms.myForm.myTextArea.value;
 var matches=value.match(regEx);
 alert(matches.length);
}

HTH,

Chris

----- Original Message -----
From: "Boloian, John" <jboloian@u...>
To: "javascript" <javascript@p...>
Sent: Monday, May 06, 2002 3:27 PM
Subject: [javascript] A better way of finding characters than looping?


> Can anyone suggest a more efficient way of doing the following: Finding
all
> the linebreaks (chr 13--this is for windows only) in a textarea?
>
> So far I'm using something along the lines of:
>
> var testLength = document.forms[0].textAreaName.value
>
> for (i=0; i < testLength.length; i++) {
> if(testLength.charCodeAt(i) == 13){
> testRows++
> }
> }
>
> ...which isn't too bad if the string is small... However things slow down
a
> bit as the string gets longer...
>
> Any suggestions appreciated! Thanks!
>
> --John Boloian




---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20

  Return to Index