Jump to content

Strings

From OpenBOR
(Redirected from Strlength())

Strings are sequences of characters used to represent text. OpenBOR Script stores strings as variants declared with the char prefix.

char message = "Hello world!";

Concatenation

The + operator concatenates strings. If either operand is a string, OpenBOR converts the other operand to text and returns a string.

char label = "Health: ";
int health = 100;

char message = label + health;

// message = "Health: 100"

Concatenation may combine strings with integers, decimals, pointers, and other variable types.

Strings containing numeric text must be converted before they can participate in arithmetic. See Strings and Numeric Conversion.

Native Functions

OpenBOR provides the following native functions for inspecting and extracting string content.

strinfirst()

Accepts a source string and search string. Returns the portion of the source beginning with the first occurrence of the search string.

Returns -1 if the search string is not found.

char haystack = "Hello World";
char needle = "llo";

mixed result = strinfirst(haystack, needle);

// result = "llo World"

strinlast()

Accepts a source string and search string. Returns the portion of the source beginning with the last occurrence of the search string.

Returns -1 if the search string is not found.

char haystack = "Hello World";
char needle = "o";

mixed result = strinlast(haystack, needle);

// result = "orld"

strleft()

Accepts a string and character count. Returns the requested number of characters from the left side of the string.

If the requested count exceeds the string length, the function returns the complete string.

char source = "Hello World";
int length = 3;

char result = strleft(source, length);

// result = "Hel"

strright()

Accepts a string and character count. Returns the requested number of characters from the right side of the string.

If the requested count exceeds the string length, the function returns the complete string.

char source = "Hello World";
int length = 3;

char result = strright(source, length);

// result = "rld"

strlength()

Accepts a string and returns its number of characters.

char source = "Hello World";

int result = strlength(source);

// result = 11

strwidth()

Accepts a string and font index. Returns the width, in pixels, required to render the string with the selected font. The result reflects the actual character widths and spacing of that font.

char source = "Hello World";
int font = 0;

int result = strwidth(source, font);