Jump to content
Sign in to follow this  
NSearch

Count characters in a string

Recommended Posts

NSearch

I can not find a function that counts the number of characters in a string.

e.g. Count the number of times that the letter T is in this sentence.

Thanks

Share this post


Link to post
Share on other sites
seandisanti

I can not find a function that counts the number of characters in a string.

e.g. Count the number of times that the letter T is in this sentence.

Thanks

here ya go.

;_StringSubLoc :: returns an array where $occ[0] = count of occurances, and each progressive index is the location within the string
#include<array.au3>
Func _StringSubLoc($ack, $blah)
    $test = StringInStr($ack,$blah)
    If $test Then
        $occ = $occ + 1
        _StringSubLoc(StringRight($ack,$test-StringLen($blah),$blah)
    EndIf
    Return($occ)
EndFunc  ;==>_StringSubLoc

Share this post


Link to post
Share on other sites
Jos

I can not find a function that counts the number of characters in a string.

e.g. Count the number of times that the letter T is in this sentence.

Thanks

alternative :

$instr = "This is an input string containing t's" 
$tempstr = StringReplace($instr,'t','')
MsgBox(0,'demo','String $instr contains ' & StringLen($instr) - StringLen($tempstr) & " t(s)")

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
seandisanti

alternative :

$instr = "This is an input string containing t's" 
$tempstr = StringReplace($instr,'t','')
MsgBox(0,'demo','String $instr contains ' & StringLen($instr) - StringLen($tempstr) & " t(s)")
nice. mine was an old udf i had, which now that i think about it could be accomplished probably faster and more efficiently just using stringsplit.

Share this post


Link to post
Share on other sites
SmOke_N

here ya go.

;_StringSubLoc :: returns an array where $occ[0] = count of occurances, and each progressive index is the location within the string
#include<array.au3>
Func _StringSubLoc($ack, $blah)
    $test = StringInStr($ack,$blah)
    If $test Then
        $occ = $occ + 1
        _StringSubLoc(StringRight($ack,$test-StringLen($blah),$blah)
    EndIf
    Return($occ)
EndFunc ;==>_StringSubLoc
Missing a bracket and defining a variable... give this a run under Seans(cameronsdad's) concept
$timer = TimerInit()
$string = 'this little man told me a story once about a drangons tail, it was quite a long tale'
MsgBox(0, '', _StringSubLoc2($string, 't') & @CRLF & TimerDiff($timer) / 1000)
Func _StringSubLoc2($ack, $blah)
    If StringInStr($ack,$blah) Then
        Local $occ = ''
        For $i = 1 To StringLen($ack)
            If StringInStr(StringMid($ack, $i, 1), $blah) Then $occ = $occ + 1
        Next
    EndIf
   Return $occ
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
seandisanti

nice. mine was an old udf i had, which now that i think about it could be accomplished probably faster and more efficiently just using stringsplit.

Func _Occurence($s_text, $s_char)
    Return UBound(StringSplit($s_text, $s_char))-2
EndFunc ;==>_Occurence
Told ya. Not sure when i messed up my _StringSubLoc(), copied it right out of my include folder, and it's always worked previously, i think that i may have accidentally corrupted it removing too many lines when i was stripping comments or something.

Share this post


Link to post
Share on other sites
Jos

One thing to remember is that the stringsplit solution is case sensitive and the stringreplace isn't so the result in this example is different:

$Instr = "This is an input string containing t's"
MsgBox(0, 'demo', 'result Occurrence1: ' & _Occurrence1($Instr, 't'))
MsgBox(0, 'demo', 'result Occurrence2: ' & _Occurrence2($Instr, 't'))
;
Func _Occurrence1($s_text, $s_char)
    Return StringLen($s_text) - StringLen(StringReplace($s_text, $s_char, ''))
EndFunc ;==>_Occurrence1
;
Func _Occurrence2($s_text, $s_char)
    Return UBound(StringSplit($s_text, $s_char)) - 2
EndFunc ;==>_Occurrence2
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
sksbir

I can not find a function that counts the number of characters in a string.

e.g. Count the number of times that the letter T is in this sentence.

Thanks

Hello

Here is my solution.

$LETTER="t"
$SAMPLESTRING="One thing to remember is that the stringsplit solution is case sensitive and the stringreplace isn't so the result in this example is different"

$TEST=StringSplit ( $SAMPLESTRING, $LETTER )
msgbox (0,"Result", "There are " & $TEST[0]-1 & " " & $LETTER & " in this string")

Share this post


Link to post
Share on other sites
seandisanti

One thing to remember is that the stringsplit solution is case sensitive and the stringreplace isn't so the result in this example is different:

$Instr = "This is an input string containing t's"
MsgBox(0, 'demo', 'result Occurrence1: ' & _Occurrence1($Instr, 't'))
MsgBox(0, 'demo', 'result Occurrence2: ' & _Occurrence2($Instr, 't'))
;
Func _Occurrence1($s_text, $s_char)
    Return StringLen($s_text) - StringLen(StringReplace($s_text, $s_char, ''))
EndFunc;==>_Occurrence1
;
Func _Occurrence2($s_text, $s_char)
    Return UBound(StringSplit($s_text, $s_char)) - 2
EndFunc;==>_Occurrence2
unless you StringLower() or StringUpper() the string and the sub

Share this post


Link to post
Share on other sites
DaveF

alternative :

$instr = "This is an input string containing t's" 
$tempstr = StringReplace($instr,'t','')
MsgBox(0,'demo','String $instr contains ' & StringLen($instr) - StringLen($tempstr) & " t(s)")
In the beta release the number of characters replaced by StringReplace is saved in @extended...

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Share this post


Link to post
Share on other sites
NSearch

Wow. Thanks everyone. I checked the forum this morning and never expected to have so many responces.

Very interesting concepts.

Thanks again.

Share this post


Link to post
Share on other sites
seandisanti

Wow. Thanks everyone. I checked the forum this morning and never expected to have so many responces.

Very interesting concepts.

Thanks again.

we're all glad to help.

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  

×