Avatar billede bennetsen Nybegynder
06. september 2007 - 10:09 Der er 3 kommentarer og
1 løsning

Tælle ord i en tekstboks

Hej jeg har brug for at tælle antallet af ord i en tekstboks

Nedenstående kode virker også hvis ordene står efter hinanden men ikke hvis de står under hinanden som her

ord1
ord2
ord3

Er der en som vil/kan hjælpe mig.

På forhånd tak
Min kode er:

    Sub AddWords()
        Dim strWhole
        Dim x
        Dim i
        Dim currentLetter
        Dim prevLetter
        Dim total
        Dim pris
       
        strWhole = Trim(form1.txtString.value) ' Remove leading and trailing spaces of string if any.
       
        if strWhole = "" Then        ' The text box is empty.
            MsgBox "Du har ikke skrevet noget tekst"
            Exit Sub
        End if
       
        x = Len(strWhole)        ' Store the total number of letters In the string.
       
        total = 1        ' Start the count at one.
       
        ' Loop through the String two letters at a time. if the current letter is a space, and the previous
        ' letter was Not a space, Then another word is counted.
        For i = 2 To x   
            currentLetter = mid(strWhole, i, 1)
            prevLetter = mid(strWhole, i - 1, 1)
            if currentLetter = Chr(32) and prevLetter <> Chr(32) Then
                total = total + 1
               
            End if
        Next
       
        MsgBox "antal ord: " & total

    End Sub
Avatar billede nielle Nybegynder
06. september 2007 - 10:14 #1
Hvad med at lave en Split() på indholdet ved mellemrums-tegene og så bruge UBound() på det resulterende array til at optælle ordene?
Avatar billede bennetsen Nybegynder
06. september 2007 - 10:20 #2
Jeg kan desvæære ikke kode i asp :-(
Fandt dette script og de kunne det meste af det jeg skulle bruge
Avatar billede bennetsen Nybegynder
06. september 2007 - 10:29 #3
Fandt en ny code som kan

Dim strScriptName
Dim strInputText

' Read in the script name so I know where to
' point the form's action to.
strScriptName = Request.ServerVariables("URL")

' Read in the input from the text area.
strInputText = Request.Form("txtWordCount")

' Check for empty input and ignore it...
If strInputText = "" Then
    strInputText = "This is some text for the textarea!"
Else
    ' Echo out the input:
    Response.Write "You entered:<br />" & vbCrLf
    Response.Write "<pre>"
    Response.Write Server.HTMLEncode(strInputText)
    Response.Write "</pre>" & vbCrLf

    ' Print out the counts we got:
    Response.Write "<p>It contained <b>" _
        & GetWordCount(strInputText) _
        & "</b> words and <b>" _
        & GetCharCount(strInputText) _
        & "</b> characters.</p><br />" & vbCrLf
End If

' I wrapped these into functions so you can reuse them.
'**** Begin Functions ***********************************
Function GetWordCount(strInput)
    Dim strTemp

    ' Deal with tabs and carriage returns
    ' by replacing them with spaces.
    strTemp = Replace(strInput, vbTab, " ")
    strTemp = Replace(strTemp, vbCr, " ")
    strTemp = Replace(strTemp, vbLf, " ")

    ' Remove leading and trailing spaces
    strTemp = Trim(strTemp)

    ' Combine multiple spaces down to single ones
    Do While InStr(1, strTemp, "  ", 1) <> 0
        strTemp = Replace(strTemp, "  ", " ")
    Loop

    ' Get a count by splitting the string into an array
    ' and retreiving the number of elements in it.
    ' I add one to deal with the 0 lower bound.
    GetWordCount = UBound(Split(strTemp, " ", -1, 1)) + 1
End Function ' GetWordCount

Function GetCharCount(strInput)
    GetCharCount = Len(strInput)
End Function ' GetCharCount
'**** End Functions *************************************
Avatar billede Slettet bruger
06. september 2007 - 15:12 #4
hejsa

hvad med bare:

<%
tekst = "denne her streng skal så indeholde noget fra en tekstboks"
response.write "Antal ord: " & ubound(split(tekst," "))
%>

- mere skal der egentlig ikke til...

\Dan
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester