4.03 Left, Right and Centre

Left

The left function returns the leftmost characters of a string.

Syntax:

left(s[, n])

where:

s
The string.
n
The number of characters to be returned


Returns:

The truncated/justified string.

Remarks:

If n is omitted one character is returned.

If n is negative the specified number of characters are removed from the right of the string.

If there are less characters in the string than the required length the output string is padded with spaces.

The LynPlex interpreter accepts numeric values for both the string and the padding character.

Example:

put left("abc", 2)        // "ab"
put left("abc", 4)        // "abc "
put left("abc", -1)       // "ab"

Implementation Status

LynPlex Interpreter
Fully implemented
LynPlexC Compiler
Implemented but numeric input not accepted
LynPlexS Compiler
Implemented but numeric input not accepted

Notes:

The right function returns the rightmost characters of a string.

Syntax:

right(s[, n[, fill]])

where:

s
The string.
n
The number of characters to be returned
fill
A string of one or more characters to be use as padding on right justification.


Returns:

The truncated/justified string.

Remarks:

If n is omitted one character is returned.

If n is negative the specified number of characters are removed from the left of the string.

If there are less characters in the string than the required length the output string is padded with spaces. If the third option is specified the value will be padded with the specified string or numeric value.

The LynPlex interpreter accepts numeric values for both the string and the padding character.

Example:

put right("abc", 2)       // "bc"
put right("abc", 4)       // " abc"
put right("abc", -1)      // "bc"
put right(99, 5)          // "   99"
put right(99, 5, 0)       // "00099"
put right("99", 5, "*")   // "***99"
put right("99", 5, "!*")  // "*!*99"

Implementation Status

LynPlex Interpreter
Fully implemented
LynPlexC Compiler
Implemented but numeric input not accepted and padding is limited to a single character
LynPlexS Compiler
Implemented but numeric input not accepted and padding is limited to a single character

Notes:

  • Padding to be implemented for left as well as right.

Mid

The mid function returns part of a string starting from a specified position. Optionally, the length to be returned can also be specified, in which case the returned string will be padded or truncated to this length.

Syntax:

mid(s, start [,length])

where:

s
The string.
start
The starting position of the substring.
length
The number of characters to be returned.


Returns:

The required substring.

Remarks:

If length is omitted or is negative, mid returns the whole string from the position start.

If there are less characters in the string than the required length the output string is padded with spaces.

If the starting position is outside the string the returned string consists solely of spaces.

Example:

put mid("abc",2,1)  // "b"
put mid("abc",2)    // "bc"
put mid("abc",3,2)  // "c "
put mid("abc",5,2)  // "  "
put mid("abc",5)    // ""

Implementation Status

LynPlex Interpreter
Fully implemented
LynPlexC Compiler
Fully implemented
LynPlexS Compiler
Fully implemented

Notes:

Centre/Center

The Centre/Center function returns the central part of a string, centred in a space filled string.

Syntax:

centre(s, n)
center(s, n)

where:

s
The string.
n
The number of characters to be returned.


Returns:

The required substring padded or truncated as necessary.

Remarks:

If returned string is always the requested length. If there are more characters in the string than the required length the string is truncated. If there are less characters in the string than the required length the output string is padded with spaces.

The two spellings of the function are for convenience. There is no difference in the way that the function works.

Example:

put centre("abc",10)  // "   abc    "
put centre("abc",11)  // "    abc    "
put centre("abc",1)   // "b"
put center("abc",2)   // "ab"

Implementation Status

LynPlex Interpreter
Fully implemented
LynPlexC Compiler
Fully implemented
LynPlexS Compiler
Fully implemented

Notes:


lynplex/lp0403.txt · Last modified: 2013/09/18 10:08 (external edit)