NSearch Posted January 31, 2006 Share Posted January 31, 2006 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 Link to comment Share on other sites More sharing options...
seandisanti Posted January 31, 2006 Share Posted January 31, 2006 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. Thankshere 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 More sharing options...
Developers Jos Posted January 31, 2006 Developers Share Posted January 31, 2006 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. Thanksalternative : $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 More sharing options...
seandisanti Posted January 31, 2006 Share Posted January 31, 2006 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 More sharing options...
Moderators SmOke_N Posted January 31, 2006 Moderators Share Posted January 31, 2006 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 More sharing options...
w0uter Posted January 31, 2006 Share Posted January 31, 2006 Func _Occurence($s_text, $s_char) Return UBound(StringSplit($s_text, $s_char))-2 EndFunc ;==>_Occurence My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll Link to comment Share on other sites More sharing options...
steve8tch Posted January 31, 2006 Share Posted January 31, 2006 I think we need a race here Link to comment Share on other sites More sharing options...
seandisanti Posted January 31, 2006 Share Posted January 31, 2006 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 More sharing options...
Developers Jos Posted February 1, 2006 Developers Share Posted February 1, 2006 (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 February 1, 2006 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 More sharing options...
sksbir Posted February 1, 2006 Share Posted February 1, 2006 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 More sharing options...
seandisanti Posted February 1, 2006 Share Posted February 1, 2006 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;==>_Occurrence2unless you StringLower() or StringUpper() the string and the sub Link to comment Share on other sites More sharing options...
DaveF Posted February 1, 2006 Share Posted February 1, 2006 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 More sharing options...
NSearch Posted February 1, 2006 Author Share Posted February 1, 2006 Wow. Thanks everyone. I checked the forum this morning and never expected to have so many responces. Very interesting concepts. Thanks again. Link to comment Share on other sites More sharing options...
seandisanti Posted February 2, 2006 Share Posted February 2, 2006 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now