Jump to content

Count characters in a String


Recommended Posts

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
Link to comment
Share on other sites

$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
Link to comment
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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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
Link to comment
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.

Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...