Jump to content

Count characters in a string


NSearch
 Share

Recommended Posts

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

  • Developers

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)")

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

  • Moderators

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.

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

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

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