I am trying to compare two strings in an IF activity condition
I am copy pasting a cell in excel, I put it into a variable. I then want to check if the value equals “EEL ftgnr”, and if it doesn’t I’ll press key down, copy paste and continue til I do.
Everything works except for the fact that no matter how I do, I can’t get the if activity to properly compare the strings.
I’ve even tried manually setting values such as numbers and text so that it doesn’t fetch anything from Excel, and I still cannot compare the strings in the if activity condition?
I’ve tried variable1=variable2 and variable1 is variable2 but can’t get it to work
Hi @guemi,
I think you can’t just compare two strings from variable. If statement requires math calculations which are not possible to do on words. Instead of this you could use some bool. Look at this code:
It returns output (true or false) and based on calculation like this you could is it for If activity like If “calculation” returs true then something…
@Ronan is correct in that the is keyword performs reference comparison, meaning that two objects with the same value will still return false when compared with is, since they are stored in different locations in memory and altering the value of one will not affect the other.
I would take this one step further and eliminate the IF statement block entirely. You should just assign the value of eel_ftgnr_hittat to the result of cell_content = eel_ftgnr.
@lakshman Also has a good point in trimming the beginning and end of any whitespace which may cause the equality to fail despite having the same content with extra spaces.
In addition to the great comments by others, keep in mind that just checking string1 = string2 will only work if it matches the case. If you don’t care whether it has upper/lowercase, then you should instead compare the two strings using String.Equals to do so and use the StringComparison options to ignore case. Therefore it’d look like this in your if condition: String.Equals(variable1,variable2,StringComparison.InvariantCultureIgnoreCase)
@guemi - If you’re copying from Excel with a CTL-C, you should know that Excel, for whatever reason, adds a CrLf at the end of of the line. This may be the issue, and you’l need to remove that - something like cell_content.replace(vbcrlf,“”), as I’m not sure where you eel_ftgnr variable is being populated
Otherwise variable1=varibale2 is a valid VB Boolean expression.
@Pablito The If condition is simply a VB (C#) expression that resolves to a Boolean value.
variable1=variable2 is a valid VB expression that resolves to True/False
@guemi Ok, I did a PoC (Proof of Concept) on my theory, and I think this proves my hypothesis. For cell_content you’re getting from Excel Read Cell Activity. That pulls just the text. But then you down arrow and use CTL+C then assign a variable the contents of the clipboard.
I used your code (modified since I didnt have your Excel file) But captured variable contents in a wrapper - observe the results: (first is cell_content, then is eel_ftgnr If Closed <DATA>test</DATA> Write Line Executing Write Line Closed <DATA>test </DATA> Write Line Executing Write Line Closed