Sign in to follow this  
Followers 0
NSearch

Count characters in a string

14 posts in this topic

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



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

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

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                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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.

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

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

[center]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.[/center]

Share this post


Link to post
Share on other sites

I think we need a race here :lmao:

Share this post


Link to post
Share on other sites

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.

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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

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

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

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

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

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.

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

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