blindwig Posted June 7, 2005 Share Posted June 7, 2005 I got tired of doing this: If $i = 1 or $i = 3 or $i = 7 or $i = 12 ... so I write a function to work like this: If _StringInSet($i, '1,3,7,12,...') And here it is: CODE Func _StringInSet($Value, $List, $Delim=',') Dim $Result=0, $ListArray, $i $ListArray=StringSplit($List,$Delim) $i = 1 While $i<=$ListArray[0] And Not $Result If $Value=$ListArray[$i] Then $Result=-1 $i = $i + 1 WEnd Return $Result EndFunc It's smarter than just using StringInStr because it won't match partials. 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...
MHz Posted June 8, 2005 Share Posted June 8, 2005 (edited) Hi blindwig, Yes, good idea. A good UDF, can save some extra typing. I would have done it a little different. Func _StringInSetOr($Value, $List, $Delim = '|') Dim $ListArray=StringSplit($List,$Delim) If @error Then Return 0 For $i = 1 To $ListArray[0] If $Value = $ListArray[$i] Then Return 1 Next EndFunc I try to avoid using comma's as delimiters. I have learnt my lesson with other languages, in the past. Accidently typing a period, somewhere in the string, makes fun debugging. But it is good that you chose as optional. Or this is with error return Func _StringInSetOr($Value, $List, $Delim = '|') Dim $ListArray=StringSplit($List,$Delim) If @error Then SetError(1) Return 0 EndIf For $i = 1 To $ListArray[0] If $Value = $ListArray[$i] Then Return 1 Next EndFunc Edited June 8, 2005 by MHz Link to comment Share on other sites More sharing options...
hou Posted April 3, 2009 Share Posted April 3, 2009 This is a very old thread, I know. However this is my way doing it ... Func StringInSet( $str, $set, $delim="|" );Tests wether string is in set of strings. Returns item no of str in set if found, 0 if not. $set= $delim & $set & $delim Local $res= StringInStr($set, $delim & $str & $delim) If $res>0 Then StringReplace( StringLeft($set,$res), $delim, " " ) Return @extended ;return replacement count (=item no) EndIf Return 0 EndFunc 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