none
SmallBasic Split Function RRS feed

  • Question

  • Hello,

    Is there a kind of Split() function in SmallBasic?
    If I have this string: "Hello, how are you", I need someting like this var.split(" ")

    So all the strings go into a 'dictionary'

    Thanks

    Sunday, September 25, 2011 9:44 AM

Answers

  • The main commands will be:

    Text.GetIndexOf to find the position of a deliminator in the string

    Text.GetSubText to get the text upto a position

    Text.GetSubTextToEnd to get the subtext remaining

    We then need to loop through the main string to look for the deliminator in a while loop and each time we find one, extract the substring and update the remaining text to process next.

    The very simplest may look like this:  Add plenty of TextWindow.WriteLines in the code to see how it works.

    input = "Hello, how are you"
    split()
    For i = 1 To Array.GetItemCount(output)
      TextWindow.WriteLine(output[i])
    EndFor
    
    Sub split
      output = "" 'Clear the output array
      inputCopy = input 'Work with a copy of the input because we will change it as we work through it
      pos = Text.GetIndexOf(inputCopy," ") 'Position of first " " - not found is 0
      While (pos > 0)
        val = Text.GetSubText(inputCopy,1,pos-1) 'Get the first word (up to pos)
        inputCopy = Text.GetSubTextToEnd(inputCopy,pos+1) 'Set the remainder of the text to process next
        i = Array.GetItemCount(output)+1 'The next index in the output array
        output[i] = val 'Set the current output to the found substring
        pos = Text.GetIndexOf(inputCopy," ") 'Check for the next position of " "
      EndWhile 'While loop ends when pos = 0 (" " not found)
      i = Array.GetItemCount(output)+1
      output[i] = inputCopy 'The remaining text is also a sub word
    EndSub
    To be more general you should think about more than one deliminator type eg comma, period etc and consider repeated spaces or spaces at the end etc.

    Sunday, September 25, 2011 10:42 AM
    Moderator

All replies

  • There are a range of text manipulation capabilities in the Text method, which can almost certainly do what you want with a little coding - not sure exactly what your split actually does, there are many ways a string may be split?

    I guess you mean return an array of words separated by " ".  You can certainly write a small subroutine to do this.

    Sunday, September 25, 2011 9:52 AM
    Moderator
  • How do I make such a sub?
    Sunday, September 25, 2011 10:15 AM
  • The main commands will be:

    Text.GetIndexOf to find the position of a deliminator in the string

    Text.GetSubText to get the text upto a position

    Text.GetSubTextToEnd to get the subtext remaining

    We then need to loop through the main string to look for the deliminator in a while loop and each time we find one, extract the substring and update the remaining text to process next.

    The very simplest may look like this:  Add plenty of TextWindow.WriteLines in the code to see how it works.

    input = "Hello, how are you"
    split()
    For i = 1 To Array.GetItemCount(output)
      TextWindow.WriteLine(output[i])
    EndFor
    
    Sub split
      output = "" 'Clear the output array
      inputCopy = input 'Work with a copy of the input because we will change it as we work through it
      pos = Text.GetIndexOf(inputCopy," ") 'Position of first " " - not found is 0
      While (pos > 0)
        val = Text.GetSubText(inputCopy,1,pos-1) 'Get the first word (up to pos)
        inputCopy = Text.GetSubTextToEnd(inputCopy,pos+1) 'Set the remainder of the text to process next
        i = Array.GetItemCount(output)+1 'The next index in the output array
        output[i] = val 'Set the current output to the found substring
        pos = Text.GetIndexOf(inputCopy," ") 'Check for the next position of " "
      EndWhile 'While loop ends when pos = 0 (" " not found)
      i = Array.GetItemCount(output)+1
      output[i] = inputCopy 'The remaining text is also a sub word
    EndSub
    To be more general you should think about more than one deliminator type eg comma, period etc and consider repeated spaces or spaces at the end etc.

    Sunday, September 25, 2011 10:42 AM
    Moderator