|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > String commands > !STR.- String Command > BASIC-String Commands > String Operations |
MiniRobotLanguage (MRL)
STR.MidStr
Extract a Substring from a Specified Position

Extract a specified number of characters from a starting position in a string.
Intention
The STR.MidStr command extracts a specified number of characters (P3) from a source string (P1), starting at a given position (P2).
The result is stored in an optional result variable (P4) or overwrites the source string if P4 is omitted.
This command is useful for extracting substrings from specific positions in text or binary data, such as parsing data or isolating segments of a string.
•The start position (P2) is 1-based and must be a positive integer.
•The number of characters (P3) must be non-negative; negative values are treated as zero.
•The command is safe for binary data, as variables in P1 are resolved only once.
If P2 is beyond the string length or P3 is zero, the result is an empty string.
Syntax
STR.MidStr|P1|P2|P3[|P4]
STR.Mid|P1|P2|P3[|P4]
Parameter Explanation
•P1 - (Source String) The string from which to extract characters.
•P2 - (Start Position) A numeric value specifying the 1-based starting position for extraction. Resolved to an integer.
•P3 - (Number of Characters) A numeric value specifying the number of characters to extract. Resolved to an integer; negative values are treated as zero.
•P4 - (Optional Result Variable) The variable to store the result. If omitted, the source string (P1) is overwritten.
Examples
'***********************************
' Example 1: Extract a substring to a result variable
'***********************************
VAR.$$SRC=HelloWorld
STR.MidStr|$$SRC|2|5|$$RES
' $$RES will contain "elloW"
MBX.$$RES
ENR.
'***********************************
' Example 2: Extract a substring, overwrite source
'***********************************
VAR.$$SRC=HelloWorld
STR.Mid|$$SRC|4|3
' $$SRC will contain "loW"
MBX.$$SRC
ENR.
'***********************************
' Example 3: Invalid position or count
'***********************************
VAR.$$SRC=HelloWorld
STR.MidStr|$$SRC|20|5|$$RES
' $$RES will contain "" (start position beyond string length)
MBX.$$RES
STR.MidStr|$$SRC|2|0|$$RES
' $$RES will contain "" (zero count)
MBX.$$RES
ENR.
Remarks
- Variables in the source string (P1) are resolved only once, ensuring safety for binary data (e.g., null characters or special sequences).
- P2 and P3 are resolved to integers; non-integer values are rounded down.
- If P2 is beyond the string length or P3 is zero or negative, an empty string is returned.
- If P2 + P3 - 1 exceeds the string length, the result includes all characters from P2 to the end of the string.
- The command is efficient for extracting substrings from specific positions in text or binary data.
Limitations:
- P2 must be a positive integer; negative or zero values result in an empty string.
- P3 must be non-negative; negative values are treated as zero.
- No case-insensitive variant available.
See also:
• STR.Left