Small Basic: How to Convert Multi-Line Text into an Array of Lines

Small Basic: How to Convert Multi-Line Text into an Array of Lines

Small Basic can get multi-line text into a variable from multi-line input text box with Controls.GetTextBoxText() or from text file with File.ReadContents().
The following sample code shows how to convert a multi-line text into an array of lines.


CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
GraphicsWindow.BackgroundColor = "LightGray"
GraphicsWindow.BrushColor = "Black"
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
tb = Controls.AddMultiLineTextBox(0, 0)
Controls.SetSize(tb, gw, gh - 30)
Controls.AddButton("Enter", gw - 50, gh - 30)
Controls.ButtonClicked = OnButtonClicked
Sub OnButtonClicked
  buf = Controls.GetTextBoxText(tb)
  ConvertTextToLines()
  ShowLines()
EndSub
Sub ConvertTextToLines
  len = Text.GetLength(buf)
  nLines = 0
  ptr = 1
  While ptr <= len
    eol = Text.GetIndexOf(Text.GetSubTextToEnd(buf, ptr), CRLF)
    If eol = 0 Then ' eol not found
      nLines = nLines + 1
      lines[nLines] = Text.GetSubTextToEnd(buf, ptr)
      ptr = len + 1
    Else ' eol found
      nLines = nLines + 1
      lines[nLines] = Text.GetSubText(buf, ptr, eol - 1)
      ptr = ptr + eol + 1
    EndIf    
  EndWhile
EndSub
Sub ShowLines
  For i = 1 To nLines
    TextWindow.WriteLine("lines[" + i + "]: " + lines[i])
  EndFor
EndSub
Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
Page 1 of 1 (3 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
Page 1 of 1 (4 items)