Sign in to follow this  
Followers 0
wanderallyouwant

Count characters in a String

12 posts in this topic

#1 ·  Posted (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 by wanderallyouwant

Share this post


Link to post
Share on other sites



But I only want letters, like not a-z. Would my above code work if my IsCharacter func existed, or is there already one?

Share this post


Link to post
Share on other sites

I guess the appropriate title should have been Count letters in a String rather than characters.

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 :whistle:

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 by xcal

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hey... gimme a break.. I just woke up :whistle:

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 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]

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by xcal

Share this post


Link to post
Share on other sites

#10 ·  Posted (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 by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

$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.

Share this post


Link to post
Share on other sites

#12 ·  Posted (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 :whistle:

Edited by Paulie

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0