StringToArray( )

Purpose
Converts a delimited string list to a string array.
 
Author Date of code (original) Updated on Date of page (original) Updated on
Bill Thoen
bthoen@ctmap.com
07Jan2001
07Jan2001
07Jan2001
09Jan2001
 
Restrictions on use
This module is distributed under the terms of the Lesser GNU General Public License. Restrictions on the use of this work in a commercial application or derivative work is described in the Lesser GNU General Public License page at: http://www.fsf.org/copyleft/lesser.html
 
Description
There are several situations when it is convenient to be able to parse a line of text into into a list of items for easier access. The most common case is converting the Mapinfo semi-colon delimited string into an array and back again. However, StringToArray can also be used to parse lines of text terminated by carriage-return/linefeed characters, or by HTML tags like "<BR>."
 
External resources
none
 
Declare statement of sub_function. Include in your program. Copy/Paste if needed.
declare sub StringToArray (
   byval sStr as string,   'Items as delimited string
   sList() as string,      'Returned items as string array
   byval sToken as string) 'Delimiter separating items in sStr
 
Returned value(s) (function only)

 
Other required declare statement(s). Include in your program. Copy/Paste if needed.
include "StringLists.def"
 
MapBasic Code. Copy/Paste if needed.
sub StringToArray (
   byval sStr as string,   'Items as delimited string
   sList() as string,      'Returned items as string array
   byval sToken as string) 'Delimiter separating items in sStr

dim i, j, p0, p1 as smallint

   j = len(sToken)
   p0 = 1
   p1 = instr(p0, sStr, sToken)
   do while p1 > 0
      i = i + 1
      redim sList(i)
      sList(i) = mid$(sStr, p0, p1-p0)
      p0 = p1 + j
      p1 = instr (p0, sStr, sToken)
   loop

   'get the last element
   if p0 < len(sStr) then
      i = i + 1
      redim sList(i)
      sList(i) = mid$(sStr, p0, Len(sStr)-p0+1)
   end if

end sub
 
Availability for download
StringLists.zip
 
Example
dim sText, sList() as string

   sText = "One;Two;Three"
   call StringToArray (sText, sList, ";")

   'sList(1) is now "One"; sList(2) = "Two", etc.

 
Comments

 
See also
ArrayToString()