If you have a string with values seperated by a delimiter character (for example: commas, line feeds, tabs, etc.) you can use the SplitString function below to extract those values into an array of string values. Once the values have been stored in the array, a ForEach loop can then be used to iterate over the individual values.
The example function below returns an array of strings. If you're not familiar with the concept of ranked arrays (which are dynamically sized arrays), read about ranked arrays here. It also uses the RegEx class in the System.Text.RegularExpressions namespace to actually parse, or split, the string. Read more about regular expressions here.
BegFunc SplitString Type( *String ) Rank( 1 )
DclSrParm StringInput Type( *String )
DclSrParm Delimiter Type( *String )
LeaveSr System.Text.RegularExpressions.Regex.Split( StringInput, Delimiter )
Figure 1: The SplitString Function
The SplitString function requires two parameters:
The string to parse.
The character being used as the delimiter.
For example, if we wanted to parse a string named MyString that was delimited with commas, the function call would be:
DclArray ResultArray Type( *String ) Rank( 1 )
ResultArray = SplitString( MyString, "," )
Figure 2: Using The SplitString Function
If the value of MyString was "1,2,3,4" ResultArray would then contain an array of four strings: "1" "2" "3" "4" after the function is executed. The delimiter character is removed when the string is parsed. If the delimiter character is a non-printable character (for example, a TAB or a linefeed) you will have to declare a constant that represents that character beforehand:
DclConst TAB Value( U'00000009' ) // Declare a TAB
Figure 3: Declaring a constant for a non-printable character.
If we wanted to parse a string called MyString delimited with tabs, using the TAB constant above, the function call would then be:
ResultArray = SplitString( MyString, TAB )
Figure 4: Using The SplitString Function with a non-printable character
Once we have executed the SplitString function, ResultArray will contain the individual values we have extracted. Using a ForEach loop it is possible to iterate through each of those values and process them as needed:
ForEach Token Type( *string ) Collection( ResultArray )
// Do something with Token
Figure 5: Iterating through a String array with a ForEach loop
NOTE: The variable "Token" does not need to be declared beforehand.
The SplitString project illustrates the use of the SplitString function, simply type something into the text field and click one of the buttons to parse the contents of the text box using either the comma, carriage return+line feed, or TAB characters as the delimiter. Message boxes will then appear for each element of ResultArray containing its value enclosed in ><, notice that spaces and newlines are maintained. Notice that if no delimiter is present, the entire string is returned.
AVR.NET 8.1 and 7.0 versions of the project are available for download via the links below, simply open the project in Visual Studio and press F5 to build and run the program.