String Operations

<< Click to Display Table of Contents >>

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

String Operations

STR.String

Previous Top Next


MiniRobotLanguage (MRL)

 

STR.String

Repeat a Character N Times

 

 

clip1121

All Tests passed.

 

 

Intention

This command creates a string consisting of multiple copies of the first character in a specified string (P1) repeated P2 times.

The result is placed on the Top of Stack (TOS) or in an optional variable (P3).

This command is similar to PowerBASIC's STRING$ function. If P2 is 0 or negative, returns an empty string. The command is binary-safe for P1, with variables resolved only once to prevent unwanted expansion.

This command is useful for creating padding, separators, or character-based patterns in text processing.

 

Schematic

Source: "abc", Count: 4 --> "aaaa"

 |a|a|a|a|

 ^ (4 copies of first character "a")

 

Syntax

STR.String|P1|P2[|P3]

 

Parameter Explanation

 

P1 - (Character String) The string from which to take the first character. Variable or literal. Binary-compatible variable resolution.

P2 - (Count) Numeric, number of times to repeat the first character. If <= 0, returns empty string.

P3 - (Optional Result Variable) Variable for result. If omitted, pushed to TOS.

 

Examples

 

' Repeat first character of "abc" 4 times

$$STR=abc

$$CNT=4

STR.String|$$STR|$$CNT

$$RES=$tos$

' $$RES = "aaaa"

 

' Repeat single character

STR.String|*|5|$$STAR

' $$STAR = "*****"

 

' Zero count returns empty string

STR.String|abc|0

$$EMPTY=$tos$

' $$EMPTY = ""

 

' Multi-character string uses only first character

STR.String|hello|3|$$HELLO

' $$HELLO = "hhh"

'-------------------------------------------------------------

 

' =================================================================

' SELF-VALIDATING TEST SCRIPT for STR.String (Repeat Character N Times)

' Purpose: Verify character repetition with various counts and edge cases.

' Uses short jumps (JIV.) and clear PRT. for readability.

' =================================================================

' Initialize counters

$$PAS=0

$$FAI=0

STS.CLEAR

PRT. ===================================================

PRT. 1. BASIC REPETITION TESTS

PRT. ===================================================

PRT. Test 1.1: Repeat first char of "abc" 4 times

$$STR=abc

$$CNT=4

STR.String|$$STR|$$CNT

$$EXP=aaaa

JIV.$tos$!$$EXP|Lab_Error1

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next1

:Lab_Error1

$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next1

STS.CLEAR

PRT. Test 1.2: Repeat single character 5 times

STR.String|*|5

$$EXP=*****

JIV.$tos$!$$EXP|Lab_Error2

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next2

:Lab_Error2

$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next2

STS.CLEAR

PRT. Test 1.3: Repeat with variable result

$$STR=xyz

$$CNT=3

STR.String|$$STR|$$CNT|$$RES

$$EXP=xxx

JIV.$$RES!$$EXP|Lab_Error3

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next3

:Lab_Error3

$$MSG= -> FAIL - Result: $$RES (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next3

STS.CLEAR

PRT. Test 1.4: Multi-character string uses only first char

STR.String|hello|3

$$EXP=hhh

JIV.$tos$!$$EXP|Lab_Error4

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next4

:Lab_Error4

$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next4

STS.CLEAR

PRT. ===================================================

PRT. 2. EDGE CASE TESTS

PRT. ===================================================

PRT. Test 2.1: Zero count returns empty string

STR.String|abc|0

$$EXP=

JIV.$tos$!$$EXP|Lab_Error5

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next5

:Lab_Error5

$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next5

STS.CLEAR

PRT. Test 2.2: Negative count returns empty string

STR.String|xyz|-2

$$EXP=

JIV.$tos$!$$EXP|Lab_Error6

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next6

:Lab_Error6

$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next6

STS.CLEAR

PRT. Test 2.3: Empty string to repeat

STR.String||5

$$EXP=

JIV.$tos$!$$EXP|Lab_Error7

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next7

:Lab_Error7

$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next7

STS.CLEAR

PRT. Test 2.4: Count of 1 returns single character

STR.String|hello|1

$$EXP=h

JIV.$tos$!$$EXP|Lab_Error8

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next8

:Lab_Error8

$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next8

STS.CLEAR

PRT. Test 2.5: Large count (100) with single char

STR.String|A|100

STR.Repeat|A|100|$$EXP

JIV.$tos$!$$EXP|Lab_Error9

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next9

:Lab_Error9

$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next9

STS.CLEAR

PRT. ===================================================

PRT. 3. BINARY DATA TEST

PRT. ===================================================

PRT. Test 3.1: Repeat binary string with null bytes (first char)

$$STR=$nul$

STR.String|$$STR|2

$$EXP=$nul$$nul$

JIV.$tos$!$$EXP|Lab_Error10

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next10

:Lab_Error10

$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

:Lab_Next10

STS.CLEAR

PRT. ===================================================

PRT. TEST SUMMARY

PRT. ===================================================

CAL.$$TOT=$$PAS+$$FAI

$$MSG=Total Tests: $$TOT

PRT.$$MSG

$$MSG=Passed: $$PAS

PRT.$$MSG

$$MSG=Failed: $$FAI

PRT.$$MSG

' Display final result

JIV.$$FAI=0|Lab_Success

$$MSG=FAILURE: $$FAI of $$TOT tests failed.

MBX.$$MSG|Test Result|16

JMP.Lab_End

:Lab_Success

MBX.SUCCESS: All tests passed!|Test Result|64

:Lab_End

ENR.

 

 

 

 

Remarks

 

-Binary-safe for P1; variables resolved only once to avoid unwanted expansion.

-P2 is resolved numerically; non-numeric values may cause errors.

-If P2 <= 0, returns empty string regardless of P1.

-Only first character of P1 is used; remaining characters ignored.

-Similar to BASIC's STRING$ function.

-No stack output if P3 is provided; use $tos$ to access TOS.

 

Limitations

 

-P2 must be numeric; invalid values may cause undefined behavior.

-No limit on repetition count, but memory constraints apply.

-Only repeats first character of P1; no multi-character support.

 

See also:

The Stack (TOS)

STR.Repeat - Repeat string N times

RPL. - RePLace in String

LEN. - Length-of-String