String validation

Hi Guys,

Not sure if anyone has covered this already.
I’d like to discuss about the string comparisons from UiPath.
Or further more variables in general.

I had experimented the following conditions:

  1. Use “is” and “isnot” to compare strings
    1.1
    Initialization for strings:
    Set default value of “string_1” to “”
    Set default value of “string_2” to “abc”

Execute the following conditions:


And the results are:
image

1.2
Then change the Initialization for strings:
Remove the default value of “string_1”
Remain the default value for “string_2”

Execute the same conditions
And the results are:
image

  1. Use “=” and “<>” to compare strings
    2.1
    Initialization for strings:
    Set default value of “string_1” to “”
    Set default value of “string_2” to “abc”

Execute the following conditions:


And the results are:
image

2.2
Then change the Initialization for strings:
Remove the default value of “string_1”
Remain the default value for “string_2”

Execute the following conditions:


And the results are:
image

What I was wondering is that why:

  • In experiment 1 where string_1 and string_2 have devault values: string_1 is “” is True, but string_2 is “abc” is False?
  • In experiment 2 where string_1 has no default value: string_1 = “” is True, also string_1 = Nothing is True?

What does empty value “” and Nothing stands for?

BR
Helliton

Hello.

I don’t know if I will answer this completely, but here it goes.

“is” and “isnot” are used when you want to compare the actual variable rather than the value of the variable. This is used mostly when comparing it to Nothing, like you would say string1 isNot Nothing, to check if the variable has been stored a value into it.

So, when looking at an empty string “”, that is the actual value that’s in the variable, rather Nothing is comparing if the variable has a value stored in it at all.

So if a variable = “”, then it has something and variable isNot Nothing will be True.

Lot of times you need to use a combination of these comparisons.

For example:

If( string1 isNot Nothing, string1.Trim <> "", False)

That example will return False if string1 is either Nothing or has an empty string.
If you were to just compare it with an empty string, then if the variable failed to store a value at all, you will get an Exception saying that there is no value in the variable.

I hope I explained that clearly.

Regards.

1 Like

Hi ClaytonM,

Thanks a lot for the answers!

In most cases, what you’ve described make sense.

However, if you look at the Experiment 1, the "string_1 = “” " and "string_1 = Nothing " both return “True” value.

So, this is a bit confusing!

BR
Helliton

“” and nothing is similar to 0 and null in ineteger.

Thank you!

BR
Helliton

Thanks a lot …
I was just looking for this …and it worked fr me…

1 Like

Could someone explaine me why I the following statement does not work in the condition activity:

variable isNot Nothing OR variable.tostring.trim<>(“”)

when I put it in the condition I always get an error “Object reference not set to an instance of an object”
Thanks in advance

Hi @Ann
You need to use inline If statement, so it checks if the variable isNot Nothing prior to checking if it is <>“”

If(variable isNot Nothing, variable.ToString.Trim <> "", False)

so you can use that in your condition, and should work.

Regards.

Hi there @Ann,
You are checking both of these conditions simultaneously:

  • variable isNot Nothing
  • variable.ToString.Trim <> (“”)

As a result, in the event the variable is not initialised, you are checking if it is not nothing (perfectly fine), but also trying to access it to trim it (not fine).

To work around this, you can do as noted by @ClaytonM above, or use the below:

variable isNot Nothing ANDALSO variable.ToString.Trim <> ("")

This ensures the second portion of the condition is only evaluated in the event the first was True. So we will only Trim the String and check it’s value, in the event it is not null.

Hope that made some sense.

1 Like

Hey, thanks so much for your explanation and help!!

Hi ClaytonM,
I am trying to check if the value is empty or null. If true then assign default value. If false - check the string length.
Your suggestion does not work unfortunately if I the string is not empty and I am trying to check the sting length then.

Your condition and the one JDavey and I presented, says if the variable is not nothing then check if the variable is not equal to empty string, else go to the false side. So, if the variable is Nothing or equals empty string, then it will go to the false side.

Let us know what exactly isn’t working and we’ll try to help. Also, verify that the variable is a value that was intended before the condition.

Regards.

1 Like