Hi
ich wollte mal wissen ob man die wörter in einem String Zählen kann.
Ich habe weder in der Hilfe noch in den Foren eine Lösung gefunden.
Das muss aber doch gehen oder?
Jemand ne Idee?
Wörter zählen
-
- [ offen ]
-
eagle -
7. Oktober 2008 um 19:48 -
Geschlossen -
Erledigt
-
-
- Offizieller Beitrag
Da man davon ausgehen kann, dass die Wörter durch Leerzeichen getrennt sind, einfach StringSplit mit Leerzeichen als Trenner.
-
Wollte ich erst machen abervorm ersten und nach dem letzten wort ist möglicherweise kein leerzeichen
-Gedankenblitz
Aber ich könnte ja mir Stringright und left gucken obe da ein Leerzeichen ist und dann 1 wort dazu rechnen.Aber was wenn mehrere Leerzeichen oder Tabs zwischen den Wörtern sind?
-
So danke für die Hilfe ich habs geschafft
Spoiler anzeigen
[autoit]
[/autoit] [autoit][/autoit] [autoit]
$string = "Ergebnis: drei Worte"$asplit = StringSplit($string, " ")
[/autoit] [autoit][/autoit] [autoit]
$icount = $asplit[0]If StringRight($string, 1) = " " Then
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]
$icount -= 1
EndIf
If StringLeft($string, 1) = " " Then
$icount -= 1
EndIfIf StringInStr($string, " ") Then
[/autoit] [autoit][/autoit] [autoit]
$strimstring = $string
While StringInStr($strimstring, " ") <> 0
If StringInStr($strimstring, " ") Then
$icount -= 1
$strimstring = StringTrimLeft($strimstring, StringInStr($strimstring, " "))
EndIf
WEnd
EndIfMsgBox(0,"",$icount)
[/autoit] [autoit][/autoit] [autoit][/autoit]Das sollte so funktoniern wenn Fehler auftreten bitte melden bei Gelegenheit mache ich ne UDF draus.
-
- Offizieller Beitrag
Muß nicht gleich 'ne UDF werden
[autoit]
Mit RegEx ist es eine Codezeile:$str = 'Das ist ein Wort und auch das ist eins.'
[/autoit]
MsgBox(0, 'Wortzahl', 'Der String enthält: ' & UBound(StringRegExp($str, '[^\s]+', 3)) & ' Worte.')
Man könnte hier noch verfeinern und definieren, was genau ein Wort ist (2 oder 3 Buchstaben min).
In der momentanen Abfrage werden auch alleinstehende Ziffern, Zeichen als Wort erfaßt. -
- Offizieller Beitrag
Genau! und \S+ sollte es auch tun
-
- Offizieller Beitrag
Du hast ja soooo recht - an die Negation durch Großbuchstaben denke ich nie, bin irgendwie auf die Kleinen orientiert.
-
Und jetzt die PREISFRAGE: "Wie oft kommt das Wort ein" in dem Satz vor?" und das mit mit einem Script nachgewiesen.
Morgens halb sieben in Deutschland. Okay 6:44 Uhr.
LG,
Crazy-A. -
- Offizieller Beitrag
[autoit]Global $s = "Hello World, I will count letters or Numbers like 1,2,3. And so on. World 111!"
[/autoit][autoit][/autoit][autoit]MsgBox(0, "", _stringCount($s, "World"))
[/autoit][autoit][/autoit][autoit]
MsgBox(0, "", _stringCount($s, "1"))
MsgBox(0, "", _stringCount($s, "num", 1)) ; Case-insensitivity flag.Func _stringCount($string, $toFind, $case = "(?-i)", $startPosition = 0)
[/autoit]
If $case = 1 Then $case = "(?i)"
Local $count = StringRegExp($string, $case & $toFind, 3, $startPosition)
Switch @error
Case 0
If IsArray($count) Then Return UBound($count)
Case 1
If Not IsArray($count) Then Return 0
Case 2
Return -1
EndSwitch
EndFunc ;==>_stringCountmit Häufigkeit
Spoiler anzeigen
[autoit]#include<Array.au3>
[/autoit] [autoit][/autoit] [autoit]
Global $str1 = '23, 129,45,67,23,768,45,878,34,67,34 / 34,45,23,57,34,789,46,78,345,768,89,34 / 12'
MsgBox(0, "Am Häufgisten oder bei Gleichheit am Größten ist :", _occurrence($str1))Func _occurrence($sString)
[/autoit] [autoit][/autoit] [autoit]
Local $splitted = StringRegExp(StringStripWS($sString, 8), '\d+', 3)
Local $result[UBound($splitted)][2]
For $i = 0 To UBound($splitted) - 1
StringReplace($str1, $splitted[$i], '')
$result[$i][1] = @extended
$result[$i][0] = Number($splitted[$i])
Next
If $result[0][1] = 1 Then Return _ArrayMax($splitted, 1, 0)
_ArraySort($result, 1, 0, 0, 1); 1, 0, 0, 2, 1)
_ArrayDblDel($result)
_ArrayDisplay($result)
Return $result[0][0]
EndFunc ;==>_occurrence;----------------------------------------------------------------------------------------------------------------------
[/autoit] [autoit][/autoit] [autoit]
; Function _ArrayDblDel(ByRef $ARRAY [, $CASESENS=0])
;
; Description - From an 1D/2D Array will delete double entries (2D -> combination by '[n][0]' and '[n][1]').
; - Autodetection 1D/2D Array
; - By using string, you can choose case sensitivity.
;
; Parameter $ARRAY: Array to sort
; optional $CASESENS: Case sensitivity off[0] or on[1] (default 0)
;
; Return Success -1 ByRef Array without doubles
; Failure 0 @error = 1, given Array is not Array
;
; Requirements Only 2 occurences in the second dimension
; #include <array.au3>
;
; Author BugFix ([email='bugfix@autoit.de'][/email])
;----------------------------------------------------------------------------------------------------------------------Func _ArrayDblDel(ByRef $ARRAY, $CASESENS = 0)
[/autoit]
If (Not IsArray($ARRAY)) Then
SetError(1)
Return 0
EndIf
Local $iDIM, $arTmp1D[1], $arTmp2D[1][2], $dbl = 0
UBound($ARRAY, 2)
If @error = 2 Then
$iDIM = 1
Else
$iDIM = 2
EndIf
$arTmp1D[0] = ""
$arTmp2D[0][0] = ""
Switch $iDIM
Case 1 ; 1D
For $i = 0 To UBound($ARRAY) - 1
$dbl = 0
For $k = 0 To UBound($arTmp1D) - 1
Switch $CASESENS
Case 0
If $arTmp1D[$k] = $ARRAY[$i] Then $dbl = 1
Case 1
If $arTmp1D[$k] == $ARRAY[$i] Then $dbl = 1
EndSwitch
Next
If $dbl = 0 Then
If $arTmp1D[0] = "" Then
$arTmp1D[0] = $ARRAY[$i]
Else
_ArrayAdd($arTmp1D, $ARRAY[$i])
EndIf
Else
$dbl = 0
EndIf
Next
$ARRAY = $arTmp1D
Case 2 ; 2D
For $i = 0 To UBound($ARRAY) - 1
$dbl = 0
For $k = 0 To UBound($arTmp2D) - 1
Switch $CASESENS
Case 0
If ($arTmp2D[$k][0] = $ARRAY[$i][0]) And _
($arTmp2D[$k][1] = $ARRAY[$i][1]) Then $dbl = 1
Case 1
If ($arTmp2D[$k][0] == $ARRAY[$i][0]) And _
($arTmp2D[$k][1] == $ARRAY[$i][1]) Then $dbl = 1
EndSwitch
Next
If $dbl = 0 Then
If $arTmp2D[0][0] = "" Then
$arTmp2D[0][0] = $ARRAY[$i][0]
$arTmp2D[0][1] = $ARRAY[$i][1]
Else
ReDim $arTmp2D[UBound($arTmp2D) + 1][2]
$arTmp2D[UBound($arTmp2D) - 1][0] = $ARRAY[$i][0]
$arTmp2D[UBound($arTmp2D) - 1][1] = $ARRAY[$i][1]
EndIf
Else
$dbl = 0
EndIf
Next
$ARRAY = $arTmp2D
EndSwitch
Return -1
EndFunc ;==>_ArrayDblDelMega
-
mh gut scheint zu funktionieren
Danke aber warum grade [^\s]+ bzw. \S+ ? -
- Offizieller Beitrag
Hi,
lad die einen Regextester runter dann siehst du es. Beides steht für alles-nicht-BLANK-Zeichen und davon 1 oder mehr. Das führt dann zu einem Treffer.
Mega