|
Splitting Strings to Sub-strings
GPP2 contains a new statement that allows splitting a string into sub-strings. Note - this is not a function and cannot be used in expressions. However, it is described here because it is part of the GPP2 string manipulation package.
STR_SPLIT <str-expr1> <str-expr2> var1, var2 ...;
The first string expression is the string to be split. It can be any string expression.
The second string expression is the delimiter sub-string. That delimiter will be used to break the first string into sub-strings. It may be any string expression and contain any number of characters (although one character is the typical delimiter).
After these two expressions comes a list of string variables, where the resulting sub-strings will be stored. At least two variables must be provided. Array members may be used (but not full arrays).
STR_SPLIT will break the source string into smaller sub-strings (based on the delimiter) and will store them in the target variables. It will use the following rules:
-
Delimiter characters that are used to separate sub-strings are removed and not copied to any target variable.
-
If the delimiter is not found at all in the source string, the entire string will be copied to the first target variable.
-
If not enough target variables are provided (too many delimiters found), the "rest" of the source string will be copied to the last target variable (including "unused" delimiters).
-
If too many target variables are provided, the rest of them will be assigned empty strings.
Examples
STR_SPLIT "abc/def" "/" var1 var2; // var1 = "abc", var2 = "def"
STR_SPLIT "abcdef" "/" var1 var2; // var1 = "abcdef", var2 = ""
STR_SPLIT "ab/cd/ef" "/" var1 var2; // var1 = "ab", var2 = "cd/ef"
STR_SPLIT "abc/def" "/" v1 v2 v3; // v1 = "abc", v2 = "def", v3 = ""
STR_SPLIT "abc||def" "||" var1 var2; // var1 = "abc", var2 = "def"
|