Comparing strings and numbers is one of the most common things you do when programming in any language. Coldfusion is no different.
Here are four of the most common ways to compare strings in Coldfusion.
NUMBERS
The above methods also work for numbers. Switch out the examples with numbers too see the output.
Here are four of the most common ways to compare strings in Coldfusion.
- IS operator
- EQ operator
- CompareNoCase function
- Compare function
METHOD 1: IS OPERATOR
Just like it sounds, you can use the IS operator.
Is variable1 equal to variable2?
This type of comparison is case insensitive.
Is variable1 equal to variable2?
This type of comparison is case insensitive.
METHOD 2: EQ OPERATOR
The EQ operator is similar to the IS operator. Again this is case insensitive.
METHOD 3: Compare Function
The compare function is used to do a case sensitive comparison of two strings. If the strings are the same this function will return 0. If the strings are different the function will return -1 if the first string is less than the second string or 1 if the first string is greater than the second string. The 1 and -1 one come in handy if you are using the compare function to compare numbers.
METHOD 4: CompareNoCase
The CompareNoCase function works the same way as Compare except it is a case insensitive search.
The EQ operator is similar to the IS operator. Again this is case insensitive.
METHOD 3: Compare Function
The compare function is used to do a case sensitive comparison of two strings. If the strings are the same this function will return 0. If the strings are different the function will return -1 if the first string is less than the second string or 1 if the first string is greater than the second string. The 1 and -1 one come in handy if you are using the compare function to compare numbers.
METHOD 4: CompareNoCase
The CompareNoCase function works the same way as Compare except it is a case insensitive search.
NUMBERS
The above methods also work for numbers. Switch out the examples with numbers too see the output.