none
String Comparisons Small Basic RRS feed

  • Question

  • Hi

    I am a teacher using Small Basic for my GCSE Computing course and I was going to demonstrate a Binary Search on a list of words.  It seems that Small Basic only compares equality but does not compute greater than or less than with strings.  Is that the case or am I missing something.  I don't really want to have to split the strings down to character codes for the comparison.

    Neil Tomas

    Wednesday, March 20, 2013 4:40 AM

Answers

  • Hi

    Thanks but it still means that if I am comparing words that are similar, i.e. Here and Hear, I have to iterate through the words until I get a mismatch.

    It really would be better if, in the next version of SB, string comparison could be added.  Perhaps made part of the Text object, returning -1, 0 and 1.

    • Marked as answer by Jasper Oaktree Wednesday, March 20, 2013 2:21 PM
    Wednesday, March 20, 2013 2:21 PM

All replies

  • SB cannot compare strings.

    But  How about this meathod?

    a="head"
    b="body"
    c=Text.GetCharacterCode(a)-text.GetCharacterCode(b) '  answer  C=6
    textwindow.WriteLine(c)

    *Note  it compares "h" and "b".
    Wednesday, March 20, 2013 7:21 AM
    Answerer
  • Hi

    Thanks but it still means that if I am comparing words that are similar, i.e. Here and Hear, I have to iterate through the words until I get a mismatch.

    It really would be better if, in the next version of SB, string comparison could be added.  Perhaps made part of the Text object, returning -1, 0 and 1.

    • Marked as answer by Jasper Oaktree Wednesday, March 20, 2013 2:21 PM
    Wednesday, March 20, 2013 2:21 PM
  • I hope for that too and many more!  :D

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, March 20, 2013 6:17 PM
    Answerer
  • One good thing about Small Basic is YOU can add this feature yourself by writing your own extension.
    Saturday, March 23, 2013 8:01 PM
  • Here's a possible solution:

    These are the same:

    a="Eric"
    b="Eric"
    c=Text.IsSubText(a, b)
    d=Text.IsSubText(b, a)
    textwindow.WriteLine(c)
    textwindow.WriteLine(d)
    textwindow.WriteLine("")
    If c And d Then
      textwindow.WriteLine("These are the same.")
    Else
      textwindow.WriteLine("These differ.")
    EndIf
      textwindow.WriteLine("")

    These are not the same:

    a="Eric"
    b="Eri"
    c=Text.IsSubText(a, b)
    d=Text.IsSubText(b, a)
    textwindow.WriteLine(c)
    textwindow.WriteLine(d)
    textwindow.WriteLine("")
    If c And d Then
      textwindow.WriteLine("These are the same.")
    Else
      textwindow.WriteLine("These differ.")
    EndIf
      textwindow.WriteLine("")

    • Proposed as answer by Emiller375 Tuesday, March 26, 2013 5:52 PM
    Monday, March 25, 2013 3:17 PM