JavaScript linebreak probleem

Pagina: 1
Acties:

  • Boudi
  • Registratie: Oktober 2000
  • Laatst online: 09-08 02:32

Boudi

Always Coca Cola

Topicstarter
OK, om een lang verhaal kort te maken:

ik moet een JSje maken dat het aantal woorden in een tekst telt. Dus ik heb het volgende gedaan:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function CountWords() {
    var I = 0;
    var cntWords = 0;
    var MyString = document.MyForm.MyText.value;

    if (MyString.length == 0)
      return 0;

    if (MyString.charAt(0) != " ") {
      if (MyString.charAt(0) != "\n")
        cntWords++;
      while (I < MyString.length && (MyString.charAt(I) != " " && MyString.charAt(I) != "\n"))
        I++;
    } /* If */

    while (I < MyString.length) {
      while (I < MyString.length && (MyString.charAt(I) == " " || MyString.charAt(I) == "\n"))
        I++;
      
      if (I == MyString.length)
        return cntWords;
      else
        cntWords++;
      
      while (I < MyString.length && (MyString.charAt(I) != " " && MyString.charAt(I) != "\n"))
        I++;
    } /* While */

    return cntWords;
}

Dat gaat best goed, maar bij het zoeken naar de eerstvolgende letter wil hij nog wel es een linebreak vinden "\n"... nou heb ik het idee dat een linebreak uit twee chars bestaat ofzo, want het tellen klopt niet helemaal... De string "1[spatie][enter]" bestaat volgens bovenstaand script uit 2 woorden en 4 karakters (MyString.length is 4) terwijl "1[spatie]" uit 1 woord en 2 karakters bestaat. Mijn gok is dus dat een [enter] uit nog een karakter bestaat, behalve de \n....

wie helpt mij?

Met of zonder mayonaise?


  • wasigh
  • Registratie: Januari 2001
  • Niet online

wasigh

wasigh.blogspot.com

meestal bestaat ie uit, \n\r
\n == newline
\r == return

  • Boudi
  • Registratie: Oktober 2000
  • Laatst online: 09-08 02:32

Boudi

Always Coca Cola

Topicstarter
Thnxs, nu doettie het...

Met of zonder mayonaise?