|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > String commands > !STR.- String Command > BASIC-String Commands > String Operations |
MiniRobotLanguage (MRL)
STR.SetMidCount
Replace a Substring with a Specified Count of Characters

Replace a specified number of characters in a string starting at a given position.
Intention
The STR.SetMidCount command replaces P3 characters in a source string (P1), starting at position P2, with a replacement string (P4), potentially altering the string’s length. The result is stored in an optional result variable (P5) or overwrites the source string if P5 is omitted. This command is useful for modifying substrings in text or binary data, such as updating specific segments in a file.
•The start position (P2) is 1-based; negative values count from the end (e.g., -1 is the last character).
•The count (P3) specifies how many characters to replace; zero skips replacement, leaving only insertion of P4.
•The command is safe for binary data, as variables in P1 and P4 are resolved only once.
If P2 is invalid or P3 is negative, no changes are made. If P4 is empty, P3 characters are deleted without insertion.
Syntax
STR.SetMidCount|P1|P2|P3|P4[|P5]
STR.Smi|P1|P2|P3|P4[|P5]
Parameter Explanation
•P1 - (Source String) The string to modify. Modified directly unless P5 is provided.
•P2 - (Start Position) A numeric value specifying the 1-based position to start replacement. Negative values count from the end (e.g., -1 is the last character). Resolved to an integer.
•P3 - (Count of Characters) A numeric value specifying how many characters to replace starting at P2. Zero skips replacement, inserting P4. Resolved to an integer.
•P4 - (Replacement String) The string to insert at P2 after removing P3 characters. Empty string deletes without insertion.
•P5 - (Optional Result Variable) The variable to store the result. If omitted, the source string (P1) is overwritten.
Examples
'***********************************
' Example 1: Replace substring in place
'***********************************
VAR.$$SRC=HelloWorld
STR.SetMidCount|$$SRC|6|5|Universe
' $$SRC will contain "HelloUniverse"
MBX.$$SRC
ENR.
'***********************************
' Example 2: Replace with negative position to result variable
'***********************************
VAR.$$SRC=HelloWorld
STR.Smi|$$SRC|-5|3|ABC|$$RES
' $$RES will contain "HelloABCrld" (position -5 is 6th character)
MBX.$$RES
ENR.
'***********************************
' Example 3: Edge cases (zero count, empty string, invalid position)
'***********************************
VAR.$$SRC=HelloWorld
STR.SetMidCount|$$SRC|6|0|ABC|$$RES
' $$RES will contain "HelloABCWorld" (zero count, insert only)
MBX.$$RES
VAR.$$SRC=
STR.SetMidCount|$$SRC|1|5|ABC
' $$SRC will contain "ABC" (empty source string)
MBX.$$SRC
STR.SetMidCount|$$SRC|20|2|XYZ|$$RES
' $$RES will contain "HelloWorld" (invalid position, unchanged)
MBX.$$RES
ENR.
Remarks
- Variables in the source (P1) and replacement (P4) strings are resolved only once, ensuring safety for binary data.
- P2 and P3 are resolved to integers; non-integer values are rounded down.
- If P2 is invalid (e.g., zero, beyond string length, or negative beyond length), no changes are made.
- If P3 is negative, it is treated as zero, resulting in insertion only.
- If P3 exceeds the remaining string length from P2, all characters from P2 to the end are replaced.
- For length-preserving replacement, use STR.SetMidCountB; for position-to-position replacement, use STR.SetMidTo.
Limitations:
- P2 must resolve to a valid position within the string length.
- P3 must be non-negative; negative values are treated as zero.
- No case-insensitive variant available.
See also: