blindwig Posted June 11, 2005 Share Posted June 11, 2005 Here's another function I wrote out of necessity. It works much like StringInStr, but instead of finding the substring in the string, it finds any of the chars from the substring in the string. Here's the code: CODE Func _StringInChr($String, $Chars, $CaseSense = 0, $Occurance = 1) Dim $i, $low=StringLen($String), $ret, $found = 0 If $Occurance = 0 Then SetError(1) Return 0 EndIf For $i = 1 to StringLen($Chars) $ret = StringInStr($String, StringMid($Chars, $i, 1), $CaseSense, $Occurance) If $ret <> 0 Then $found = -1 If $ret < $low Then $low = $ret EndIf Next If $Found Then Return $low Else Return 0 EndIf EndFunc My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
JSThePatriot Posted June 11, 2005 Share Posted June 11, 2005 Just as a note. I havent acutally looked the function over very well, but I think to go along with a logical naming method it should be CharInStr(). Though I noticed you used StringInStr() in your function. What exactly does yoru function do that StringInStr() doesnt do? I would love an explanation. Just my thoughts to this point, JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
ryeguy Posted June 11, 2005 Share Posted June 11, 2005 It says above his code. Link to comment Share on other sites More sharing options...
blindwig Posted June 11, 2005 Author Share Posted June 11, 2005 Just as a note. I havent acutally looked the function over very well, but I think to go along with a logical naming method it should be CharInStr().Yeah, I debated myself over what to call it. CharInStr makes more sense from an English point of view, but going by the convention (module)(function), since it has to do with strings, it should be in the string module, and I guess it would make more sense to call it StringChrIn, but I went with the convention to match the StringInStr function.Though I noticed you used StringInStr() in your function. What exactly does yoru function do that StringInStr() doesnt do? I would love an explanation.Instead of looking for a substring in the string, in looks for characters in the string. It goes through each character in the substring and looks for that character in the string.Oops, looking at it now, I see a big logic problem if you try to use $Occurance. I'll do a rewrite and get ack to you... My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
JSThePatriot Posted June 11, 2005 Share Posted June 11, 2005 Yeah, I debated myself over what to call it. CharInStr makes more sense from an English point of view, but going by the convention (module)(function), since it has to do with strings, it should be in the string module, and I guess it would make more sense to call it StringChrIn, but I went with the convention to match the StringInStr function.Instead of looking for a substring in the string, in looks for characters in the string. It goes through each character in the substring and looks for that character in the string.Oops, looking at it now, I see a big logic problem if you try to use $Occurance. I'll do a rewrite and get ack to you...<{POST_SNAPBACK}>I know what your function does, but StringInStr() can also find the characters in a string? Your function goes through and numbers how many occurances of the letter?StringInStr() = SubString in String is how I took it. Which is why I said Char in String. You understand that as you said. I am still not sure why you would want it the other way around. It is a string function, but it would just go into the string.au3 header file. I had never heard that the function names are under a (module)(function) though that makes sense. Its your function I was just offering a suggestion.JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
/dev/null Posted June 11, 2005 Share Posted June 11, 2005 how about StringContainsChar() or StringIsSuperSetofChar() Cheers Kurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
Helge Posted June 11, 2005 Share Posted June 11, 2005 I like StringCharsInStr(), however it doesn't exactlyexplain what it does.. StringGetAllChars() ? Link to comment Share on other sites More sharing options...
/dev/null Posted June 11, 2005 Share Posted June 11, 2005 StringFindAnyChar() ?? Cheers Kurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
Insolence Posted June 11, 2005 Share Posted June 11, 2005 StringFindAnyChar is the most 'AutoIt' "I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar. Link to comment Share on other sites More sharing options...
blindwig Posted June 13, 2005 Author Share Posted June 13, 2005 I know what your function does, but StringInStr() can also find the characters in a string? Your function goes through and numbers how many occurances of the letter?<{POST_SNAPBACK}>You're still not quite getting it:StringInStr('abc','xyz') will look for 'xyz' in 'abc' and return it's positionStringFindAnyChar('abc','xyz') (if that's what I end up calling it) will look for 'x' or 'y' or 'z' in 'abc'I had never heard that the function names are under a (module)(function) though that makes sense. Its your function I was just offering a suggestion.<{POST_SNAPBACK}>Think about it, all string-related functions start with string:StringUpperStringTrimStringIsANumetcAll file-related functions start with File, all GUI related functions start with GUI, etc...And thanks for the suggestion. The whole reason I post here is for peer review and suggestions to make thing work better. My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
JSThePatriot Posted June 13, 2005 Share Posted June 13, 2005 You're still not quite getting it:StringInStr('abc','xyz') will look for 'xyz' in 'abc' and return it's positionStringFindAnyChar('abc','xyz') (if that's what I end up calling it) will look for 'x' or 'y' or 'z' in 'abc'Think about it, all string-related functions start with string:StringUpperStringTrimStringIsANumetcAll file-related functions start with File, all GUI related functions start with GUI, etc...And thanks for the suggestion. The whole reason I post here is for peer review and suggestions to make thing work better. <{POST_SNAPBACK}>Okay I understand. I just thought everything was logically named That is why I would have said CharInStr(), but StringFindChars() would be fine I think.Sounds good and somewhat descriptive.JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
blindwig Posted June 13, 2005 Author Share Posted June 13, 2005 OK, here is the function, re-written from scratch: CODE Func _StringFindChr($String, $Chars, $CaseSense = 0, $Occurance = 1) Dim $i = 0, $j, $s1, $sl = StringLen($String), $c1, $cl = StringLen($Chars), $Found If $Occurance > 0 Then ;Start from left $SearchDir = 1 $i = 1 ElseIf $Occurance < 0 Then ;Start from right $SearchDir = -1 $i = $sl Else ;error SetError(1) Return 0 EndIf ;loop through the string While 0 < $i And $i <= $sl and $Occurance <> 0 $s1 = StringMid($String, $i, 1) $Found = 0 ;loop through the characters $j = 1 While $j <= $cl and not $Found $c1 = StringMid($Chars, $j, 1) If $s1 == $c1 or ($s1 = $c1 and $CaseSense = 0) Then $Found = -1 Else $j = $j + 1 EndIf WEnd If $Found Then $Occurance = $Occurance - $SearchDir EndIf If $Occurance <> 0 Then $i = $i + $SearchDir WEnd if $i > $sl Then $i = 0 Return $i EndFunc And this is how it works, for example: _StringFindAnyChr('the quick brown fox jumped over the lazy dog','aeiou',0,3) this will find the 3rd occurance of any vowel (regardless of case) in the given string and return it's location. My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
cal Posted June 16, 2005 Share Posted June 16, 2005 OK, here is the function, re-written from scratch:Thanks. I've being trying to write something like this today using StringInStr without alot of luck. This is more then I need but your loops within it are exacty what I was looking for. Link to comment Share on other sites More sharing options...
blindwig Posted June 16, 2005 Author Share Posted June 16, 2005 Thanks. I've being trying to write something like this today using StringInStr without alot of luck. This is more then I need but your loops within it are exacty what I was looking for.<{POST_SNAPBACK}>What is it that you're trying to do?Maybe my _StringInSet would be better suited to your task? My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
cal Posted June 20, 2005 Share Posted June 20, 2005 What is it that you're trying to do?Maybe my _StringInSet would be better suited to your task?<{POST_SNAPBACK}>Nothing to fancy. I had a small script that took tv listings from TvTome and stripped them to just a ep number and title. I thought I'd redo it to work with Tv.com now that Tome is no more.Tome had a very easy format to change. A few ctrl arrows and ctrlshiftarrows cut the lines down to exactly as I wanted them. tv.com has a different format, I need to search for numbers (skipping the ep numbers) and then backtrack to find the end of the title keeping any numbers in the (x) format as they are part of the title. Nothing to difficult.Now that I've thought about it, there are several other ways of doing this as well.I'll take a better look at your other code as well. Thanks. Link to comment Share on other sites More sharing options...
Baku Posted July 29, 2005 Share Posted July 29, 2005 Really nice idea, really nice udf. Thank you 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