06. september 2007 - 10:09Der 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
' 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 *************************************
Synes godt om
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
Synes godt om
Ny brugerNybegynder
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.