STR_CUT, MID, SUBSTR
The STR_CUT, MID and SUBSTR functions copy characters of a string and lets you truncate the resulting string. Use these functions when you want to copy all or parts of strings. Define which character to begin copying from, and optionally define the number of characters you want to copy.
These script functions all serve a similar purpose, which is to copy a part of a string. Define the String that you want to copy the characters from, and which character in that string to begin copying from Start. Optionally specify the Length if you only want to copy a certain number of characters. If you do not specify a length, the function returns all characters until the end of the string.
Note: You can specify an absolute length that exceeds the number of characters that are available in the string. In this case, the functions return the defined characters with additional blanks that are appended. If necessary, you can truncate these blanks with the STR_RTRIM function.
The original string itself remains unchanged, as you store the new string in a variable.
Syntax
STR_CUT (String, Start[, Length])
MID (String, Start[, Length])
SUBSTR (String, Start[, Length])
Parameters
- STR_CUT, MID, or SUBSTR
Copy the characters of a string
- String
Alphanumeric string
Format: script literal or script variable
- Start
Start position from which to start copying
Format: number without quotation marks or script variable
- Length
(Optional) The number of characters that should be copied
Format: number without quotation marks or script variable
Return code: The specified part of the string
Examples
The following script returns all characters starting with the fourth character until the end of the string. The result is system.
:SET &PARTOFSTRING# = SUBSTR("AE system", 4)
The following example returns two characters, starting with the third character of the string. The result is CD.
:SET &STRING# = MID("ABCDEFGH",3,2)
The following example returns all characters starting with the third character. The returned result is CDEFGH.
:SET &STRING# = SUBSTR("ABCDEFGH",3)
The following example sets a length that exceeds the number of characters that are available in the string. Therefore, the function appends blanks for the missing characters. The result is >>CDEFGH <<.
:SET &STRING# = STR_CUT("ABCDEFGH",3,10)
:PRINT ">>&STRING#<<"
The following script splits a user-defined term. The first three characters are assigned to the first script variable and the last character to the second script variable.
:READ &TABNAME#, "04", "Please specify table
name xxxy"
:SET &TABPRE# = SUBSTR(&TABNAME#,1,3)
:SET &TABSUF# = SUBSTR(&TABNAME#,4,1)
See also: