Topic
  • 3 replies
  • Latest Post - ‏2014-03-26T15:25:51Z by Tony_Goodman
CMusicFan
CMusicFan
17 Posts

Pinned topic Comparing strings alphabetically

‏2014-03-26T14:42:24Z |

Greetings,

This seems like a very basic question to me, but I have not been able to find a clear answer in the dxl reference manual. I am wishing to compare two strings alphabetically. I was thinking about creating a function that uses the ASCII values of each character in the strings to compare them to each other letter by letter. I have read the section on string comparison in the reference manual, but it is not clear if it is comparing string length, string order, or some other arbitrary thing. Here is the text that I am finding confusing:

 

String relational operators can be used as shown in the following syntax:
string s1 == string s2
string s1 != string s2
string s1 < string s2l
string s1 > string s2
string s1 <= string s2
string s1 >= string s2
These operators return true if s1 is equal, not equal, less than, greater than, less than or equal to, or greater than or equal to s2. Case is significant.
Example
print ("aaaa" < "a" ) // prints "false"
print ("aaaa" > "a" ) // prints "true"
print ("aaaa" == "a" ) // prints "false"
print ("A" > "a" ) // prints "false"
print ("McDonald" < "Man" ) // prints "false"

The above text comes straight from the reference manual, but it doesn't state what one string being greater than another one means. If this is a good way to compare strings alphabetically, please let me know!

Thanks!

  • Tony_Goodman
    Tony_Goodman
    270 Posts
    ACCEPTED ANSWER

    Re: Comparing strings alphabetically

    ‏2014-03-26T15:07:11Z  

    Case is significant, but your examples do not demonstrate this. Try

    print ("B" < "a")  // prints true

    So the comparison is NOT alphabetical, it is according to ASCII value.

    If you want to compare alphabetically, you will need to take the case of the string out of the equation by using cistrcmp(s1, s2).

    An alternative might be to use upper(s) or lower(s2)

    Hope that helps

  • Tony_Goodman
    Tony_Goodman
    270 Posts

    Re: Comparing strings alphabetically

    ‏2014-03-26T15:07:11Z  

    Case is significant, but your examples do not demonstrate this. Try

    print ("B" < "a")  // prints true

    So the comparison is NOT alphabetical, it is according to ASCII value.

    If you want to compare alphabetically, you will need to take the case of the string out of the equation by using cistrcmp(s1, s2).

    An alternative might be to use upper(s) or lower(s2)

    Hope that helps

  • CMusicFan
    CMusicFan
    17 Posts

    Re: Comparing strings alphabetically

    ‏2014-03-26T15:11:14Z  

    Case is significant, but your examples do not demonstrate this. Try

    print ("B" < "a")  // prints true

    So the comparison is NOT alphabetical, it is according to ASCII value.

    If you want to compare alphabetically, you will need to take the case of the string out of the equation by using cistrcmp(s1, s2).

    An alternative might be to use upper(s) or lower(s2)

    Hope that helps

    I think it does. What I was really wanting to know was what "greater than" actually means in this context. All of the above cases could refer to string length and still be true. I was unsure if "greater than" meant greater in ascii value, or greater in string length, or greater in some other aspect.

    Thanks!

  • Tony_Goodman
    Tony_Goodman
    270 Posts

    Re: Comparing strings alphabetically

    ‏2014-03-26T15:25:14Z  
    • CMusicFan
    • ‏2014-03-26T15:11:14Z

    I think it does. What I was really wanting to know was what "greater than" actually means in this context. All of the above cases could refer to string length and still be true. I was unsure if "greater than" meant greater in ascii value, or greater in string length, or greater in some other aspect.

    Thanks!

    I see, for that we have the length function.

     

    print (length("AAA") > length("BB"))   // prints true

  • Tony_Goodman
    Tony_Goodman
    270 Posts

    Re: Comparing strings alphabetically

    ‏2014-03-26T15:25:51Z  
    • CMusicFan
    • ‏2014-03-26T15:11:14Z

    I think it does. What I was really wanting to know was what "greater than" actually means in this context. All of the above cases could refer to string length and still be true. I was unsure if "greater than" meant greater in ascii value, or greater in string length, or greater in some other aspect.

    Thanks!

    I see, for that we have the length function.

     

    print (length("AAA") > length("BB"))   // prints true