locked
how do i find something in text RRS feed

  • Question

  • Ok, I love the language, but something's been a bit of a pain for me, and i cant find it in the documentation....

    If i have a string, how do i get the program to recognise formatting.

    For example, if i want the program to interpret a list, and break it down:

    "sausages, ham, bacon"

    How do i get the program to recognise the comma as a list seperator,

    or lets say i want the length of a string, or want it to split off every second char into a seperate var...

    Thursday, February 11, 2010 12:35 PM

Answers

  • Ok, I love the language, but something's been a bit of a pain for me, and i cant find it in the documentation....

    If i have a string, how do i get the program to recognise formatting.

    For example, if i want the program to interpret a list, and break it down:

    "sausages, ham, bacon"

    How do i get the program to recognise the comma as a list seperator,

    or lets say i want the length of a string, or want it to split off every second char into a seperate var...


    Choo, not too sure what you're looking for exactly, but...

    To split a string up based on a certain sub-string, you'd do something like this:

    split=","
    
    string = "one,two,three,four"
    result = ""
    splitIt()
    
    For item=1 to Array.GetItemCount(result)
      TextWindow.WriteLine(result[item])
    endfor
    
    
    
    Sub splitIt
    While Text.IsSubText(string, split)
      result[Array.GetItemCount(result) + 1] = Text.GetSubText(string, 1, Text.GetIndexOf(string, split) - 1)
      string = Text.GetSubTextToEnd(string, Text.GetIndexOf(string, split) + 1)
    EndWhile
    EndSub
    Notice above that it generates an array (named 'result') from the initial string, so to re-use the split values you just reference them in the array.

    To split the string up every second char would have to either be done using an array to hold all the 2 char results, or you'd have to do it manually if you wanted to put the values into actual variables, like...

    string = "aabbccddeeff"

    a = text.getSubText(string, 1, 2)
    b = text.getSubText(string, 3, 2)
    etc.

    Anyway, I hope that is somewhat close to what you wanted. Or maybe you're asking for some sort of RegularExpression feature, which SB doesn't have.
    • Marked as answer by Vijaye Raji Sunday, February 14, 2010 12:54 AM
    Saturday, February 13, 2010 1:41 AM

All replies

  • chootastic,

    Check the reply to http://social.msdn.microsoft.com/Forums/en-US/smallbasic/thread/7d22f434-34b1-48e7-ad61-62a1cfc1fd54 and import CJJ410 as an example of parsing a comma separated list and some of the SmallBasic Text commands manipulating strings.
    Thursday, February 11, 2010 4:58 PM
  • Thursday, February 11, 2010 5:41 PM
  • Ok, I love the language, but something's been a bit of a pain for me, and i cant find it in the documentation....

    If i have a string, how do i get the program to recognise formatting.

    For example, if i want the program to interpret a list, and break it down:

    "sausages, ham, bacon"

    How do i get the program to recognise the comma as a list seperator,

    or lets say i want the length of a string, or want it to split off every second char into a seperate var...


    Choo, not too sure what you're looking for exactly, but...

    To split a string up based on a certain sub-string, you'd do something like this:

    split=","
    
    string = "one,two,three,four"
    result = ""
    splitIt()
    
    For item=1 to Array.GetItemCount(result)
      TextWindow.WriteLine(result[item])
    endfor
    
    
    
    Sub splitIt
    While Text.IsSubText(string, split)
      result[Array.GetItemCount(result) + 1] = Text.GetSubText(string, 1, Text.GetIndexOf(string, split) - 1)
      string = Text.GetSubTextToEnd(string, Text.GetIndexOf(string, split) + 1)
    EndWhile
    EndSub
    Notice above that it generates an array (named 'result') from the initial string, so to re-use the split values you just reference them in the array.

    To split the string up every second char would have to either be done using an array to hold all the 2 char results, or you'd have to do it manually if you wanted to put the values into actual variables, like...

    string = "aabbccddeeff"

    a = text.getSubText(string, 1, 2)
    b = text.getSubText(string, 3, 2)
    etc.

    Anyway, I hope that is somewhat close to what you wanted. Or maybe you're asking for some sort of RegularExpression feature, which SB doesn't have.
    • Marked as answer by Vijaye Raji Sunday, February 14, 2010 12:54 AM
    Saturday, February 13, 2010 1:41 AM

  • I got more deep understanding about this part, Thanks for your explanation!
    Wednesday, January 26, 2011 11:44 PM