|
Functions for dealing with string values. More...
Modules | |
Field Manipulators | |
Functions to deal with whitespace-separated lists of values in strings. | |
Functions | |
string | collapseEscape (string text) |
Replace all escape sequences in text with their respective character codes. | |
void | dumpStringMemStats () |
Dumps information about String memory usage. | |
bool | endsWith (string str, string suffix, bool caseSensitive=false) |
Test whether the given string ends with the given suffix. | |
string | expandEscape (string text) |
Replace all characters in text that need to be escaped for the string to be a valid string literal with their respective escape sequences. | |
string | getSubStr (string str, int start, int numChars=-1) |
Return a substring of str starting at start and continuing either through to the end of str (if numChars is -1) or for numChars characters (except if this would exceed the actual source string length). | |
int | getTrailingNumber (string str) |
Get the numeric suffix of the given input string. | |
bool | isalnum (string str, int index) |
Test whether the character at the given position is an alpha-numeric character. | |
bool | isspace (string str, int index) |
Test whether the character at the given position is a whitespace character. | |
string | ltrim (string str) |
Remove leading whitespace from the string. | |
string | nextToken (string str, string token, string delimiters) |
Tokenize a string using a set of delimiting characters. | |
string | rtrim (string str) |
Remove trailing whitespace from the string. | |
bool | startsWith (string str, string prefix, bool caseSensitive=false) |
Test whether the given string begins with the given prefix. | |
int | strasc (string chr) |
Return the integer character code value corresponding to the first character in the given string. | |
string | strchr (string str, string chr) |
Find the first occurrence of the given character in str. | |
int | strchrpos (string str, string chr, int start=0) |
Find the first occurrence of the given character in the given string. | |
int | strcmp (string str1, string str2) |
Compares two strings using case-sensitive comparison. | |
string | strformat (string format, string value) |
Format the given value as a string using printf-style formatting. | |
int | stricmp (string str1, string str2) |
Compares two strings using case-insensitive comparison. | |
int | strinatcmp (string str1, string str2) |
Compares two strings using "natural order" case-insensitive comparison. | |
string | stripChars (string str, string chars) |
Remove all occurrences of characters contained in chars from str. | |
String | stripTrailingNumber (string str) |
Strip a numeric suffix from the given string. | |
bool | strIsMatchExpr (string pattern, string str, bool caseSensitive=false) |
Match a pattern against a string. | |
bool | strIsMatchMultipleExpr (string patterns, string str, bool caseSensitive=false) |
Match a multiple patterns against a single string. | |
int | strlen (string str) |
Get the length of the given string in bytes. | |
string | strlwr (string str) |
Return an all lower-case version of the given string. | |
int | strnatcmp (string str1, string str2) |
Compares two strings using "natural order" case-sensitive comparison. | |
int | strpos (string haystack, string needle, int offset=0) |
Find the start of needle in haystack searching from left to right beginning at the given offset. | |
string | strrchr (string str, string chr) |
Find the last occurrence of the given character in str. | |
int | strrchrpos (string str, string chr, int start=0) |
Find the last occurrence of the given character in the given string. | |
string | strrepeat (string str, int numTimes, string delimiter="") |
Return a string that repeats str numTimes number of times delimiting each occurrence with delimiter. | |
string | strreplace (string source, string from, string to) |
Replace all occurrences of from in source with to. | |
int | strstr (string string, string substring) |
Find the start of substring in the given string searching from left to right. | |
string | strupr (string str) |
Return an all upper-case version of the given string. | |
string | trim (string str) |
Remove leading and trailing whitespace from the string. |
Functions for dealing with string values.
Since in TorqueScript any value is implicitly also a string, these functions can be used with all values.
string collapseEscape | ( | string | text | ) |
Replace all escape sequences in text with their respective character codes.
This function replaces all escape sequences (\n, \t, etc) in the given string with the respective characters they represent.
The primary use of this function is for converting strings from their literal form into their compiled/translated form, as is normally done by the TorqueScript compiler.
text | A string. |
// Print: // // str // ing // // to the console. Note how the backslash in the string must be escaped here // in order to prevent the TorqueScript compiler from collapsing the escape // sequence in the resulting string. echo( collapseEscape( "str\ning" ) );
void dumpStringMemStats | ( | ) |
Dumps information about String memory usage.
bool endsWith | ( | string | str, | |
string | suffix, | |||
bool | caseSensitive = false | |||
) |
Test whether the given string ends with the given suffix.
str | The string to test. | |
suffix | The potential suffix of str. | |
caseSensitive | If true, the comparison will be case-sensitive; if false, differences in casing will not be taken into account. |
startsWith( "TEST123", "123" ) // Returns true.
string expandEscape | ( | string | text | ) |
Replace all characters in text that need to be escaped for the string to be a valid string literal with their respective escape sequences.
All characters in text that cannot appear in a string literal will be replaced by an escape sequence (\n, \t, etc).
The primary use of this function is for converting strings suitable for being passed as string literals to the TorqueScript compiler.
text | A string |
expandEscape( "str" NL "ing" ) // Returns "str\ning".
string getSubStr | ( | string | str, | |
int | start, | |||
int | numChars = -1 | |||
) |
Return a substring of str starting at start and continuing either through to the end of str (if numChars is -1) or for numChars characters (except if this would exceed the actual source string length).
str | The string from which to extract a substring. | |
start | The offset at which to start copying out characters. | |
numChars | Optional argument to specify the number of characters to copy. If this is -1, all characters up the end of the input string are copied. |
getSubStr( "foobar", 1, 2 ) // Returns "oo".
int getTrailingNumber | ( | string | str | ) |
Get the numeric suffix of the given input string.
str | The string from which to read out the numeric suffix. |
getTrailingNumber( "test123" ) // Returns '123'.
bool isalnum | ( | string | str, | |
int | index | |||
) |
Test whether the character at the given position is an alpha-numeric character.
Alpha-numeric characters are characters that are either alphabetic (a-z, A-Z) or numbers (0-9).
str | The string to test. | |
index | The index of a character in str. |
bool isspace | ( | string | str, | |
int | index | |||
) |
Test whether the character at the given position is a whitespace character.
Characters such as tab, space, or newline are considered whitespace.
str | The string to test. | |
index | The index of a character in str. |
string ltrim | ( | string | str | ) |
string nextToken | ( | string | str, | |
string | token, | |||
string | delimiters | |||
) |
Tokenize a string using a set of delimiting characters.
This function first skips all leading charaters in str that are contained in delimiters. From that position, it then scans for the next character in str that is contained in delimiters and stores all characters from the starting position up to the first delimiter in a variable in the current scope called token. Finally, it skips all characters in delimiters after the token and then returns the remaining string contents in str.
To scan out all tokens in a string, call this function repeatedly by passing the result it returns each time as the new str until the function returns "".
str | A string. | |
token | The name of the variable in which to store the current token. This variable is set in the scope in which nextToken is called. | |
delimiters | A string of characters. Each character is considered a delimiter. |
string rtrim | ( | string | str | ) |
bool startsWith | ( | string | str, | |
string | prefix, | |||
bool | caseSensitive = false | |||
) |
Test whether the given string begins with the given prefix.
str | The string to test. | |
prefix | The potential prefix of str. | |
caseSensitive | If true, the comparison will be case-sensitive; if false, differences in casing will not be taken into account. |
startsWith( "TEST123", "test" ) // Returns true.
int strasc | ( | string | chr | ) |
Return the integer character code value corresponding to the first character in the given string.
chr | a (one-character) string. |
string strchr | ( | string | str, | |
string | chr | |||
) |
Find the first occurrence of the given character in str.
str | The string to search. | |
chr | The character to search for. Only the first character from the string is taken. |
int strchrpos | ( | string | str, | |
string | chr, | |||
int | start = 0 | |||
) |
Find the first occurrence of the given character in the given string.
str | The string to search. | |
chr | The character to look for. Only the first character of this string will be searched for. | |
start | The index into str at which to start searching for the given character. |
strchrpos( "test", "s" ) // Returns 2.
int strcmp | ( | string | str1, | |
string | str2 | |||
) |
Compares two strings using case-sensitive comparison.
str1 | The first string. | |
str2 | The second string. |
string strformat | ( | string | format, | |
string | value | |||
) |
Format the given value as a string using printf-style formatting.
format | A printf-style format string. | |
value | The value argument matching the given format string. |
// Convert the given integer value to a string in a hex notation. %hex = strformat( "%x", %value );
int stricmp | ( | string | str1, | |
string | str2 | |||
) |
Compares two strings using case-insensitive comparison.
str1 | The first string. | |
str2 | The second string. |
int strinatcmp | ( | string | str1, | |
string | str2 | |||
) |
Compares two strings using "natural order" case-insensitive comparison.
Natural order means that rather than solely comparing single character code values, strings are ordered in a natural way. For example, the string "hello10" is considered greater than the string "hello2" even though the first numeric character in "hello10" actually has a smaller character value than the corresponding character in "hello2". However, since 10 is greater than 2, strnatcmp will put "hello10" after "hello2".
str1 | The first string. | |
str2 | The second string. |
// Bubble sort 10 elements of %array using natural order do { %swapped = false; for( %i = 0; %i < 10 - 1; %i ++ ) if( strnatcmp( %array[ %i ], %array[ %i + 1 ] ) > 0 ) { %temp = %array[ %i ]; %array[ %i ] = %array[ %i + 1 ]; %array[ %i + 1 ] = %temp; %swapped = true; } } while( %swapped );
string stripChars | ( | string | str, | |
string | chars | |||
) |
Remove all occurrences of characters contained in chars from str.
str | The string to filter characters out from. | |
chars | A string of characters to filter out from str. |
stripChars( "teststring", "se" ); // Returns "tttring".
String stripTrailingNumber | ( | string | str | ) |
Strip a numeric suffix from the given string.
str | The string from which to strip its numeric suffix. |
stripTrailingNumber( "test123" ) // Returns "test".
bool strIsMatchExpr | ( | string | pattern, | |
string | str, | |||
bool | caseSensitive = false | |||
) |
Match a pattern against a string.
pattern | The wildcard pattern to match against. The pattern can include characters, '*' to match any number of characters and '?' to match a single character. | |
str | The string which should be matched against pattern. | |
caseSensitive | If true, characters in the pattern are matched in case-sensitive fashion against this string. If false, differences in casing are ignored. |
strIsMatchExpr( "f?o*R", "foobar" ) // Returns true.
bool strIsMatchMultipleExpr | ( | string | patterns, | |
string | str, | |||
bool | caseSensitive = false | |||
) |
Match a multiple patterns against a single string.
patterns | A tab-separated list of patterns. Each pattern can include charaters, '*' to match any number of characters and '?' to match a single character. Each of the patterns is tried in turn. | |
str | The string which should be matched against patterns. | |
caseSensitive | If true, characters in the pattern are matched in case-sensitive fashion against this string. If false, differences in casing are ignored. |
strIsMatchMultipleExpr( "*.cs *.gui *.mis", "mymission.mis" ) // Returns true.
int strlen | ( | string | str | ) |
Get the length of the given string in bytes.
str | A string. |
string strlwr | ( | string | str | ) |
int strnatcmp | ( | string | str1, | |
string | str2 | |||
) |
Compares two strings using "natural order" case-sensitive comparison.
Natural order means that rather than solely comparing single character code values, strings are ordered in a natural way. For example, the string "hello10" is considered greater than the string "hello2" even though the first numeric character in "hello10" actually has a smaller character value than the corresponding character in "hello2". However, since 10 is greater than 2, strnatcmp will put "hello10" after "hello2".
str1 | The first string. | |
str2 | The second string. |
// Bubble sort 10 elements of %array using natural order do { %swapped = false; for( %i = 0; %i < 10 - 1; %i ++ ) if( strnatcmp( %array[ %i ], %array[ %i + 1 ] ) > 0 ) { %temp = %array[ %i ]; %array[ %i ] = %array[ %i + 1 ]; %array[ %i + 1 ] = %temp; %swapped = true; } } while( %swapped );
int strpos | ( | string | haystack, | |
string | needle, | |||
int | offset = 0 | |||
) |
Find the start of needle in haystack searching from left to right beginning at the given offset.
haystack | The string to search. | |
needle | The string to search for. |
strpos( "b ab", "b", 1 ) // Returns 3.
string strrchr | ( | string | str, | |
string | chr | |||
) |
Find the last occurrence of the given character in str.
str | The string to search. | |
chr | The character to search for. Only the first character from the string is taken. |
int strrchrpos | ( | string | str, | |
string | chr, | |||
int | start = 0 | |||
) |
Find the last occurrence of the given character in the given string.
str | The string to search. | |
chr | The character to look for. Only the first character of this string will be searched for. | |
start | The index into str at which to start searching for the given character. |
strrchrpos( "test", "t" ) // Returns 3.
string strrepeat | ( | string | str, | |
int | numTimes, | |||
string | delimiter = "" | |||
) |
Return a string that repeats str numTimes number of times delimiting each occurrence with delimiter.
str | The string to repeat multiple times. | |
numTimes | The number of times to repeat str in the result string. | |
delimiter | The string to put between each repetition of str. |
strrepeat( "a", 5, "b" ) // Returns "ababababa".
string strreplace | ( | string | source, | |
string | from, | |||
string | to | |||
) |
Replace all occurrences of from in source with to.
source | The string in which to replace the occurrences of from. | |
from | The string to replace in source. | |
to | The string with which to replace occurrences of . |
strreplace( "aabbccbb", "bb", "ee" ) // Returns "aaeeccee".
int strstr | ( | string | string, | |
string | substring | |||
) |
Find the start of substring in the given string searching from left to right.
string | The string to search. | |
substring | The string to search for. |
strstr( "abcd", "c" ) // Returns 2.
string strupr | ( | string | str | ) |
string trim | ( | string | str | ) |
Remove leading and trailing whitespace from the string.
str | A string. |
trim( " string " ); // Returns "string".