|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > String commands > !STR.- String Command > Split String Commands > String Operations |
MiniRobotLanguage (MRL)
STR.SplitString
Split String into Two Parts at First Delimiter Occurrence

Intention
The STR.SplitString command splits a source string (P1) into two parts at the first occurrence of a specified single-character delimiter (P2). The left part (before the delimiter) is stored in a variable (P3) or pushed to the Top of Stack (TOS), and the right part (after the delimiter) is stored in a variable (P4) or pushed to the TOS. If both P3 and P4 are omitted, both parts are pushed to the TOS (right part first, then left part).
The search is case-sensitive, and the delimiter must be a single character. If the delimiter is not found, the entire string is returned as the left part, and the right part is empty. This command is useful for simple string splitting tasks, such as parsing key-value pairs or file paths. For extracting fields by index with multiple delimiters, use STR.Parse or STR.ParseAny.
Schematic (String Splitting)
Source: Hello,World
Delimiter: , --> Left: Hello, Right: World
|H|e|l|l|o|,|W|o|r|l|d|
^ ^ ^ ^ (Split at first ,)
Source: path/to/file.txt
Delimiter: / --> Left: path, Right: to/file.txt
|p|a|t|h|/|t|o|/|f|i|l|e|.|t|x|t|
^ ^ ^ ^ (Split at first /)
Syntax
STR.SplitString|P1|P2[|P3][|P4]
Parameter Explanation
•P1 - (Source String) The string to be split. Variable or literal.
•P2 - (Delimiter) Single-character delimiter used to split the string. Variable or literal.
•P3 - (Optional Left Part Variable) Variable to store the left part (before delimiter). If omitted, left part is pushed to TOS.
•P4 - (Optional Right Part Variable) Variable to store the right part (after delimiter). If omitted, right part is pushed to TOS.
Examples
' Split string with comma delimiter to variables
STS.CLEAR
$$SRC=Hello,World
$$DLT=,
STR.SplitString|$$SRC|$$DLT|$$LEF|$$RIG
PRT.$$LEF = Hello, $$RIG = World
' Split string with slash delimiter to TOS
STS.CLEAR
$$SRC=path/to/file.txt
$$DLT=/
STR.SplitString|$$SRC|$$DLT
POP.$$LEF
POP.$$RIG
STS.CLEAR
PRT.$$LEF = path, $$RIG = to/file.txt
' Split string with no delimiter to variables
STS.CLEAR
$$SRC=HelloWorld
$$DLT=,
STR.SplitString|$$SRC|$$DLT|$$LEF|$$RIG
MBX.$$LEF = HelloWorld, <$$RIG>
ENR.
Remarks
-Case-sensitive search for the delimiter.
-P2 must be a single character; multi-character delimiters are not supported.
-Result excludes the delimiter.
-If the delimiter is not found, P3 (or TOS) receives the entire string, and P4 (or TOS) is empty.
-If both P3 and P4 are omitted, both parts are pushed to TOS (right part first, then left part).
-Binary-safe for P1 and P2; no special/system vars expanded.
-For extracting fields by index, use STR.Parse or STR.ParseAny.
-Note: Implementation may leave unintended items on the stack; use STS.CLEAR after execution.
Limitations
-Only supports single-character delimiters; for multi-character delimiters, use STR.Parse.
-Does not support nested delimiters or quoted fields; use STR.GetNested or STR.GrabQuoted for those cases.
-Splits only at the first delimiter occurrence; for multiple splits, use STR.Parse or STR.ParseAny.
-Current implementation may corrupt stack; clear stack after use.
See also: