none
Boolean compare fails when comparing text characters RRS feed

  • Question

  • The following bit of code always executes the ELSE branch.  The unicode value for A is 65, for lower case a is 97.  Even stranger, the comparison operators, > and = also fail.  I'm trying to write a sort.  He only examples I've found sort integers. How does one compare character strings? 

    If(  ("A"< "a") ) Then
      TextWindow.Write("A is smaller than a" )
      Else
      TextWindow.WriteLine("compare A < a failed" )
    EndIf

    Help is appreciated

    russ wolfe

    Tuesday, March 3, 2015 6:18 PM

Answers

  • You have to compare the charactercodes:

    If Text.GetCharacterCode("A") < Text.GetCharacterCode("a") Then 


    Jan [ WhTurner ] The Netherlands

    Tuesday, March 3, 2015 6:36 PM
    Answerer
  • I didn't find your particular problem (using Text.GetCharacterCode() and storing them arrays) documented anywhere instead I found information about unicode in general. Here are the links:

    http://social.technet.microsoft.com/wiki/contents/articles/25196.small-basic-character-set-unicode.aspx

     

    http://smallbasic.com/doc.aspx?o=Text

     

    If you wish, you can document this particular situation by contributing a wiki article here (click link "post article" at top right corner of page in link below):

    http://social.technet.microsoft.com/wiki/contents/articles/17553.wiki-small-basic-portal.aspx

     

    I'm not sure if there are any requirements for posting an article or how elaborate each article may be.

    I advise inquiring further with forum moderators if you are interested.




    Wednesday, March 4, 2015 3:00 AM

All replies

  • You have to compare the charactercodes:

    If Text.GetCharacterCode("A") < Text.GetCharacterCode("a") Then 


    Jan [ WhTurner ] The Netherlands

    Tuesday, March 3, 2015 6:36 PM
    Answerer
  • Hi,

    The Small Basic support only equality and non equality on the strings.

    This program prove it :

    If ("A" < "a") Then
      TextWindow.WriteLine("A < a")
    EndIf
    If ("A" > "a") Then
      TextWindow.WriteLine("A > a")
    EndIf
    If ("A" = "a") Then
      TextWindow.WriteLine("A = a")
    EndIf
    If ("A" <> "a") Then
      TextWindow.WriteLine("A <> a")
    EndIf
    
    If ("a" < "a") Then
      TextWindow.WriteLine("a < a")
    EndIf
    If ("a" > "a") Then
      TextWindow.WriteLine("a > a")
    EndIf
    If ("a" = "a") Then
      TextWindow.WriteLine("a = a")
    EndIf
    If ("a" <> "a") Then
      TextWindow.WriteLine("a <> a")
    EndIf
    

    display only :

    A <> a
    a = a
    

    As WhTurner proposed, use the "Text.GetCharacterCode()" function.

    Regards,


    Yan Grenier

    Merci de bien vouloir "Marquer comme réponse", les réponses qui ont répondues à votre question, et de noter les réponses que vous avez trouvé utiles.

    Tuesday, March 3, 2015 8:28 PM
    Answerer
  • Thank you for the replies.  After I posted my question, I continued to experiment, and found that indeed the only boolean comparisons that work corectly are <> and =. Using the Text.GetCharacterCode() method. only returns code for one character at a time.  In order to compare two strings of arbitrary length,one would have to build two arrays of unicode values.  Tedious at the least.

    Is this limitation documented anyplace?

    thanks again for the replies. 

    russ wolfe

    Tuesday, March 3, 2015 10:05 PM
  • I didn't find your particular problem (using Text.GetCharacterCode() and storing them arrays) documented anywhere instead I found information about unicode in general. Here are the links:

    http://social.technet.microsoft.com/wiki/contents/articles/25196.small-basic-character-set-unicode.aspx

     

    http://smallbasic.com/doc.aspx?o=Text

     

    If you wish, you can document this particular situation by contributing a wiki article here (click link "post article" at top right corner of page in link below):

    http://social.technet.microsoft.com/wiki/contents/articles/17553.wiki-small-basic-portal.aspx

     

    I'm not sure if there are any requirements for posting an article or how elaborate each article may be.

    I advise inquiring further with forum moderators if you are interested.




    Wednesday, March 4, 2015 3:00 AM