String Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > String commands > !STR.- String Command > BASIC-String Commands >

String Operations

STR.MidTo

Previous Top Next


MiniRobotLanguage (MRL)

 

STR.MidTo

Extract a Substring Between Specified Positions

 

clip1146

Extract characters from a source string between a start and end position.

 

Intention

 

The STR.MidTo command extracts a substring from a source string (P1) between a start position (P2) and an end position (P3), inclusive.

The result is stored in an optional result variable (P4) or overwrites the source string if P4 is omitted.

This command is useful for isolating specific segments of text or binary data, such as extracting a word or a data field from a string.

The range is inclusive, extracting characters from P2 to P3.

Both P2 and P3 are 1-based positions and must be positive integers.

The command is safe for binary data, as variables in P1 are resolved only once.

If P2 or P3 is invalid (e.g., zero, negative, beyond string length, or P2 > P3), the result is an empty string.

 

Syntax

 

STR.MidTo|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 - (End Position) A numeric value specifying the 1-based ending position for extraction (inclusive). Resolved to an integer.

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.MidTo|$$SRC|2|6|$$RES

' $$RES will contain "elloWo"

MBX.$$RES

ENR.

 

'***********************************

' Example 2: Extract a substring, overwrite source

'***********************************

VAR.$$SRC=HelloWorld

STR.MidTo|$$SRC|4|6

' $$SRC will contain "loWo"

MBX.$$SRC

ENR.

 

'***********************************

' Example 3: Invalid range

'***********************************

VAR.$$SRC=HelloWorld

STR.MidTo|$$SRC|6|4|$$RES

' $$RES will contain "" (invalid range, P2 > P3)

MBX.$$RES

STR.MidTo|$$SRC|20|25|$$RES

' $$RES will contain "" (start position beyond string length)

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, P3 is zero or negative, or P2 > P3, an empty string is returned.

- If P3 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.

- For extracting a specified number of characters, use STR.MidStr.

 

Limitations:

- P2 and P3 must be positive integers; negative or zero values result in an empty string.

- P2 must not exceed P3, or the result is an empty string.

- No case-insensitive variant available.

 

See also:

STR.MidStr

STR.Left

STR.Right

STR.Retain

STR.CiRetain

STR.RetainAny

STR.Clone

STR.Delete

STR.DeleteTo

RPL. - RePLace in String