Sign in to follow this  
Followers 0
huldu

String compare multiple values?

11 posts in this topic

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



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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                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

#5 ·  Posted (edited)

$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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                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

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

#10 ·  Posted (edited)

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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                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  
Followers 0