Jump to content
Sign in to follow this  
huldu

String compare multiple values?

Recommended Posts

huldu

I was wondering how you compare multiple values in a string?

$a = "Test, Test2, Test3, Test4"
$b = "Test2"

If $a = $b Then
 ConsoleWrite("True" & @CR)
Else
 ConsoleWrite("False" & @CR)
EndIf

Of course the above doesnt work, but is there a way to make it work without having to declare a new variable each time? For i am trying to compare a value given with 10 other predefined values. Right now

my code looks something like this: IF $a = "Test1" or $a = "Test2" or $a = "Test3" and so on... But there must be a better way to do it?:x


"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites
Jos

I have used it this way before:

$a = "|Test|Test2|Test3|Test4|"
$b = "Test2"
If StringInStr($a, "|" & $b & "|") Then
    ConsoleWrite("True" & @CR)
Else
    ConsoleWrite("False" & @CR)
EndIf

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

Share this post


Link to post
Share on other sites
Jos

$a = "Test, Test2, Test3, Test4"
$b = "Test2"

If StringInStr($a,$b) Then
 ConsoleWrite("True" & @CR)
Else
 ConsoleWrite("False" & @CR)
EndIf

There is a False Positive risk in this way of testing. :x Edited by Jos

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

Share this post


Link to post
Share on other sites
huldu

Thank you Zedna works like a charm, exactly what i was looking for :x


"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites
Jos

Something to do with the commas?

no, all to do with delimiting. This would also be true: $b = "st2" or $b = "e"

Jos :x

Edited by Jos

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

Share this post


Link to post
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
Sign in to follow this  

×