String Operations

<< Click to Display Table of Contents >>

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

String Operations

STR.Reverse

Previous Top Next


MiniRobotLanguage (MRL)

 

STR.Reverse

Reverse the Characters in a String

 

Reverse string

Reverse the order of all characters in a source string.

 

Intention

 

The STR.Reverse command reverses the order of all characters in a source string (P1), placing the last character first and the first character last. The result is stored in an optional result variable (P2) or overwrites the source string if P2 is omitted. This command is useful for reversing text or binary data, such as creating palindromes, reversing byte sequences, or processing data in reverse order.

The entire string is reversed, maintaining its original length.

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

An empty string remains empty after reversal.

If P1 is empty, the result is an empty string.

 

Syntax

 

STR.Reverse|P1[|P2]

 

Parameter Explanation

 

P1 - (Source String) The string to be reversed. Modified directly unless P2 is provided.

P2 - (Optional Result Variable) The variable to store the reversed string. If omitted, the source string (P1) is overwritten.

 

Examples

 

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

' Example 1: Reverse a string in place

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

VAR.$$SRC=HelloWorld

STR.Reverse|$$SRC

' $$SRC will contain "dlroWolleH"

MBX.$$SRC

ENR.

 

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

' Example 2: Reverse a string to a result variable

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

VAR.$$SRC=ABC

STR.Reverse|$$SRC|$$RES

' $$RES will contain "CBA"

MBX.$$RES

ENR.

 

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

' Example 3: Reverse an empty or single-character string

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

VAR.$$SRC=

STR.Reverse|$$SRC|$$RES

' $$RES will contain "" (empty string)

MBX.$$RES

VAR.$$SRC=A

STR.Reverse|$$SRC

' $$SRC will contain "A" (single character unchanged)

MBX.$$SRC

ENR.

 

Remarks

- Variables in the source string (P1) are resolved only once, ensuring safety for binary data (e.g., null characters or special sequences).

- The command is efficient for reversing strings of any length, including large binary data.

- Single-character strings remain unchanged, and empty strings return empty.

- For extracting or manipulating specific parts of a string, consider STR.MidStr, STR.Left, or STR.Right.

 

Limitations:

- No option to reverse a specific substring; the entire string is reversed.

- No case-insensitive variant available.

 

See also:

STR.MidStr

STR.Left

STR.Right

STR.MidTo

STR.Overwrite

STR.Patch

STR.Delete

STR.DeleteTo

STR.Retain

STR.CiRetain

STR.RetainAny

STR.Clone

RPL. - RePLace in String