wanderallyouwant Posted July 20, 2007 Posted July 20, 2007 (edited) Looking for something like this: Func _LettersInString($string) Dim $result, $i ;<not sure> of the following $letters = StringSplit($string) for $i = 0 to UBound($letters) if IsCharacter($letters[$i]) then $result = $result + 1 Next ;</not sure> Return $result EndFunc $sample = "Hey, what's for lunch at 12?" MsgBox(0,"","There are " & _LettersInString($sample) & " letters in the string.") Edited July 20, 2007 by wanderallyouwant
xcal Posted July 20, 2007 Posted July 20, 2007 StringLen. And use StringStripWS if you don't want the spaces. How To Ask Questions The Smart Way
wanderallyouwant Posted July 20, 2007 Author Posted July 20, 2007 But I only want letters, like not a-z. Would my above code work if my IsCharacter func existed, or is there already one?
wanderallyouwant Posted July 20, 2007 Author Posted July 20, 2007 I guess the appropriate title should have been Count letters in a String rather than characters.
xcal Posted July 20, 2007 Posted July 20, 2007 (edited) $str = 'Hey, what''s for lunch at 12?' $ret = StringRegExp($str, '[^\d\W]', 3) MsgBox(0, '', UBound($ret)) edit2 - undid my original edit, since I had it right in the first place Changed the pattern to be more consistant, too. Maybe a regexp expert could tell me why [^\d\W] works, but [\D\w] doesn't? They should be the same, no? Edited July 20, 2007 by xcal How To Ask Questions The Smart Way
SpookMeister Posted July 20, 2007 Posted July 20, 2007 I believe UBound is only for arrays Instead of UBound, use StringLen in your script example like xcal mentioned above [u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]
xcal Posted July 20, 2007 Posted July 20, 2007 You realize it's returning an array for each letter, right? How To Ask Questions The Smart Way
SpookMeister Posted July 20, 2007 Posted July 20, 2007 (edited) Hey... gimme a break.. I just woke up This might work for him too [edit.. realized he wanted a numerical return] $string = "Happy 16th Birthday" MsgBox(4096,"",$string & @CRLF & _LettersInString($string)) Func _LettersInString($sString) $output = "" For $i = 1 To StringLen($sString) $char = StringMid($sString, $i, 1) If Not Number($char) Then $output = $output & $char Next $output = StringStripWS($output, 8) Return StringLen($output) EndFunc ;==>_LettersInString Edited July 20, 2007 by SpookMeister [u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]
xcal Posted July 20, 2007 Posted July 20, 2007 (edited) Or you could also do... $str = 'Hey, what''s for lunch at 12?' $ret = StringRegExpReplace($str, '[\d\W]', '') MsgBox(0, '', StringLen($ret)) ; edit - Or this probably makes the most sense... $str = 'Hey, what''s for lunch at 12?' $ret = StringRegExp($str, '(?i)[a-z]', 3) MsgBox(0, '', UBound($ret))oÝ÷ ØéÚéZµçg¢×è^âëÊÍ=Û"Z®¢ÙµÈZ*®¢Ü(Ú®¢×!j¶µêÚÌh®éÝz»¢·ªº[ºÛay·¥£hzÉ÷öÜ(®F®¶sbb33c·7G"Òb33´WÂvBb33²b33·2f÷"ÇVæ6B#òb33°¢b33c·&WBÒ7G&æu&VtWb33c·7G"Âb33²b3#·rb33²Â2 ¤×6t&÷Âb33²b33²ÂT&÷VæBb33c·&WB It also returns the 1 and the 2. Did I misunderstand something? Edited July 20, 2007 by xcal How To Ask Questions The Smart Way
Siao Posted July 20, 2007 Posted July 20, 2007 (edited) $sample = "Hey, what's for lunch at 12?" MsgBox(0,"","There are " & _LettersInString($sample) & " letters in the string.") Func _LettersInString($string) Return StringLen(StringRegExpReplace($string, '[^[:alpha:]]', '')) EndFunc Edited July 20, 2007 by Siao "be smart, drink your wine"
wanderallyouwant Posted July 21, 2007 Author Posted July 21, 2007 $sample = "Hey, what's for lunch at 12?" MsgBox(0,"","There are " & _LettersInString($sample) & " letters in the string.") Func _LettersInString($string) Return StringLen(StringRegExpReplace($string, '[^[:alpha:]]', '')) EndFunc Works great, how can I make it work for numbers? I tried changing alpha to numeric, numeral and all forms of the word I could think of.
Paulie Posted July 21, 2007 Posted July 21, 2007 (edited) Works great, how can I make it work for numbers? I tried changing alpha to numeric, numeral and all forms of the word I could think of.Here is how you would do for Numbers $sample = "So the 2 movies start at 1:00, 2:00, and 4:00?" MsgBox(0,$Sample,"There are " & _NumbersInString($sample) & " numbers in the string.") Func _NumbersInString($string) Return StringLen(StringRegExpReplace($string, '[^[:digit:]]', '')) EndFunc The helpfile is like Prego. Its in there Edited July 21, 2007 by Paulie
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now