Single line expression/formula for matching the string from comma separated list

35 Views Asked by At

I have following string which is a comma separated list

Dim str1, str2

str1="C_adminToolsChartsOnFormsManager,C_adminToolsFormDesignerCopyFormDesign,C_adminToolsManageLocationsAccessSubSystem,C_adminToolsManageLocationsAddLocation,C_adminToolsManageLocationsCopyLocation"
str2="C_adminToolsFormDesigner"

I want to search str2 in str1 with exact match. C_adminToolsFormDesigner should not match with C_adminToolsFormDesignerCopyFormDesign.

I have used below single expression but it always returns true. It should do the exact match and should return false.

IIF(InStr(str1,str2),"True","False")

Can anyone help me in writing the single line expression?

1

There are 1 best solutions below

0
trincot On

You could check for the commas, like so:

result = IIf(InStr("," & str1 & ",", "," & str2 & ","), "True", "False")

NB: I'm not sure why you would want True and False as String data type. It seems more natural to produce a Boolean value:

result = InStr("," & str1 & ",", "," & str2 & ",") > 0