MultiScript - String Functions
The following string functions are supported in MultiScript:
| StrLen | Return the length of a string. |
| StrSub | Return a substring from a string. |
| StrFind | Return the position of a substring in a string. |
| StrRFind | Return the position of a substring using reverse find (back-to-front search). |
| StrReplace | Replace a substring in a string. |
| StrReplaceChars | Replace all characters found with one new character. |
| StrReplaceCharsPairs | Replace all characters found with a new characters. |
| StrToUpper | Return a string in uppercase. |
| StrToLower | Return a string in lowercase. |
| StrTrim | Return a string that has been trimmed of unwanted characters both at the beginning and end. |
| StrTrimLeft | Return a string that has been trimmed of unwanted characters from the left. |
| StrTrimRight | Return a string that has been trimmed of unwanted characters from the right. |
| StrSplit | Split a string and return the separated substrings as an array. |
| StrSplitAnyOf | Split a string on any delimiter it finds and return the separated substrings as an array. |
| StrCompareNoCase | Return the difference of two strings ignoring case differences. |
| operator == | Return the difference of two strings (case-sensitive comparison). |
| StrIsEqual | Return 1 if two strings are equal, otherwise 0 (case-sensitive). |
| StrIsEqualNoCase | Return 1 if two strings are equal, ignoring case differences. |
| StrIsWildMatch | Return 1 if two strings match using a case-sensitive wildcard algorithm. |
| StrIsWildMatchNoCase | Return 1 if two strings match using a case-insensitive wildcard algorithm. |
| StrIsRegExpMatch | Return 1 if two strings match using regular expressions (case-sensitive). |
| StrIsRegExpMatchNoCase | Return 1 if two strings match using regular expressions, ignoring case difference. |
| StrRegExpFind | Find a substring using a regular expression. |
| StrRegExpReplace | Find a substring using a regular expression and replace it using regexp. |
| IsNumeric | Return 1 if a string can be parsed as a number. |
| StrStartsWith | Return 1 if a string starts with a given prefix. |
| StrEndsWith | Return 1 if a string ends with a given suffix. |
| StrContains | Return 1 if a string contains a given substring. |
| StrFormat | Format a string using printf-style format specifiers. |
| StrPadLeft | Pad a string on the left to a minimum width. |
| StrPadRight | Pad a string on the right to a minimum width. |
| StrCount | Count the number of non-overlapping occurrences of a substring. |
| StrRepeat | Repeat a string a given number of times. |
See also
SaveStringToFile
LoadStringFromFile
StrLen
Return the length of a string.
<num> StrLen( <str> string );
Parameters
- string
- A string to get the length of
Return Value
The number of characters in string
Example
@var $str = "MyString"; @var $len = StrLen( $str ); // $len is 8
StrSub
Return a substring beginning at a specified location and having a specified length
<str> StrSub(<str> input, <num> offset, <num> length);
Parameters
- input
- A string from which the substring is extracted
- offset
- Initial position to begin the search (starting with 0)
- length
- The number of characters to include in the returned substring, or -1 to include all characters through to the end of the string input
Return Value
A string that is the substring of input beginning at the specified location
StrFind
Find the position of a substring in a string (case-sensitive).
<num> StrFind(<str> input, <str> find, <num> offset);
Parameters
- input
- The string to be searched
- find
- The string to be found
- offset
- Initial position to begin the search (starting with 0)
Return Value
The position in the input string in which the search string find is found (the first character position is 0), or -1 if find is not found
StrRFind
Find the position of a substring in a string, searching the string in reverse (case-sensitive).
<num> StrRFind(<str> input, <str> find [, <num> offset]);
Parameters
- input
- The string to be searched
- find
- The string to be found
- offset
- (Optional) Position (0-based from the start) at which the reverse scan begins. If omitted, the scan starts from the end of the string.
Return Value
The position in the input string in which the search string find is found (the first character position is 0), or -1 if find is not found
StrReplace
Replace a substring within a string (case-sensitive by default).
<str> StrReplace(<str> input, <str> find, <str> with [, <str> options] );
Parameters
- input
- A string within which to replace the substring
- find
- The substring to be replaced
- with
- The string that the substring find should to be replaced with
- options
- (Optional) Pass
"ignorecase"to perform a case-insensitive replacement
Return Value
The string input with the substring find replaced by the string with
Example
@var $str = "MyString"; @var $str2 = StrReplace( $str, "My" , "New" ); // $str2 is "NewString" @var $str3 = StrReplace( $str, "my", "New", "ignorecase" ); // $str3 is "NewString" (case-insensitive match)
StrReplaceChars
Replace a any specified character with one new character (case-sensitive).
<str> StrReplaceChars(<str> input, <str> find, <str> with );
Parameters
- input
- A string within which to replace the substring
- find
- A string of character to find
- with
- The character that any of the found characters should be replaced with
Return Value
The string input with the characters replaced, If any. If no characters was replace the string is returned unmodified
Example
@var $str = "MyString"; @var $str2 = StrReplaceChars( $str, "ryg" , "_" ); // $str2 is "M_St_in_"
StrReplaceCharsPairs
Replace a any specified character with a matching characters (case-sensitive).
the find and with parameter must be same in length. Ex character 2 in "find" will when found be replaced with character 2 from "with"
<str> StrReplaceCharsPairs(<str> input, <str> find, <str> with );
Parameters
- input
- A string within which to replace the substring
- find
- A string of character to find
- with
- The string of characters to replace a found character with
Return Value
The string input with the characters replaced, If any. If no characters was replace the string is returned unmodified
Example
@var $str = "MyString"; @var $str2 = StrReplaceChars( $str, "ryg" , "_23" ); // $str2 is "M2St_in3"
StrToUpper
Transform a string into all uppercase characters.
<str> StrToUpper(<str> input);
Parameters
- input
- A string that should be transformed
Return Value
An uppercase version of the string input
StrToLower
Transform a string into all lowercase characters.
<str> StrToLower(<str> input);
Parameters
- input
- A string that should be transformed
Return Value
A lowercase version of the string input
StrTrim
Remove unwanted characters from both the left and right side of a string (case-sensitive).
<str> StrTrim(<str> input, <str> trimchars);
Parameters
- input
- A string that should be trimmed
- trimchars
- A string specifying all characters to be removed
Return Value
A string where all characters in trimchars have been removed from both the left side (beginning) and the right side (end) of the string input
StrTrimLeft
Remove unwanted characters from the left side of a string (case-sensitive).
<str> StrTrimLeft(<str> input, <str> trimchars);
Parameters
- input
- A string that should be trimmed
- trimchars
- A string specifying all characters to be removed
Return Value
A string in which all characters in trimchars have been removed from the left side (beginning)
StrTrimRight
Remove unwanted characters from the right side of a string (case-sensitive).
<str> StrTrimRight(<str> input, <str> trimchars);
Parameters
- input
- A string that should be trimmed
- trimchars
- A string specifying all characters to be removed
Return Value
A string in which all characters in trimchars have been removed from the right side (end)
StrSplit
Split a string into an array of strings (case-sensitive).
<array> StrSplit(<str> input, <str> delimiter);
Parameters
- input
- A string that should be split
- delimiter
- A string containing the character(s) that will be used as a delimiter
Return Value
The string input split into multiple strings in an array of type string. The string is split wherever the character(s) in the string delimiter are located
If the characters in delimiter are not found, the original string input is returned
StrSplitAnyOf
Split a string on any of the delimiters into an array of strings (case-sensitive).
<array> StrSplitAnyOf(<str> input, <str> delimiters);
Parameters
- input
- A string that should be split
- delimiters
- A string containing the characters that will be used as a delimiter. It will split on ANY of the specified characters
Return Value
The string input split into multiple strings in an array of type string. The string is split wherever any of the character(s) in the string delimiter are located
If the characters in delimiter are not found, the original string input is returned
StrCompareNoCase
Compare two strings, ignoring case differences.
<num> StrCompareNoCase(<str> string1, <str> string2);
Parameters
- string1
- First string to compare
- string2
- Second string to compare
Return Value
A number showing the differences between the strings string1 and string2. The return value indicates the lexicographic relation of string1 to string2
< 0 if string1 is less than string2.
0 if string1 is equal to string2.
> 0 if string1 is greater than string2.
Operator ==
Compare two strings (case-sensitive).
<num> <str> == <str>;
Parameters
- string1
- First string to compare
- string2
- Second string to compare
Return Value
1 if the string are the same, and 0 if the are different.
Example
@var $str1 = "ABCDEF"; @var $str2 = "ABCDEF"; @var $val = $Str1 == $Str2; // $val == 1 @var $str3 = "AbcDeF"; @var $val = $Str2 == $Str3; // $val == 0
StrIsEqual
Check whether two strings are equal (case-sensitive).
<num> StrIsEqual(<str> string1, <str> string2);
Parameters
- string1
- First string to compare
- string2
- Second string to compare
Return Value
A value of 1 if string1 is equal to string2, 0 if the strings are not identical
StrIsEqualNoCase
Check whether two strings are Equal, ignoring case differences.
<num> StrIsEqualNoCase(<str> string1, <str> string2);
Parameters
- string1
- First string to compare
- string2
- Second string to compare
Return Value
A value of 1 if string1 is equal to string2, 0 if the strings are not identical (ignoring case)
StrIsWildMatch
Check if a string matches a wildcard criterion (case-sensitive).
Wildcard is a string containing "*" (for anything) and "?" (for any single character).
<num> StrIsWildMatch(<str> input, <str> wildcard);
Parameters
- input
- A string to check
- wildcard
- A wildcard string to check against
Return Value
1 if wildcard matches the string input, 0 if no match is found
Examples
@var $str = "123def567"; // If the character sequence "def" is in the string $str, $val1 will be 1 @var $val1 = StrIsWildMatch( $str , "*def*"); // The two first characters can be anything, // But the third character must be a 3. // And anything can be after it. then $val1 will be 1 @var $val1 = StrIsWildMatch( $str , "??3*");
StrIsWildMatchNoCase
Check if a string matches wildcard criteria, ignoring case differences.
Wildcard is a string containing "*" (for anything) and "?" (for any single character).
<num> StrIsWildMatchNoCase(<str> input, <str> wildcard);
Parameters
- input
- A string to check
- wildcard
- A wildcard string to check against
Return Value
1 if wildcard matches the string input, 0 if no match is found
StrIsRegExpMatch
Check if a string matches a regular expression (case-sensitive).
<num> StrIsRegExpMatch(<str> input, <str> regexp);
Parameters
- input
- A string to check
- regexp
- A regular expression to check against
Return Value
1 if the regular expression regexp matches the string input, 0 if no match is found
StrIsRegExpMatchNoCase
Check if a string matches a regular expression, ignoring case differences.
<num> StrIsRegExpMatchNoCase(<str> input, <str> regexp);
Parameters
- input
- A string to check
- regexp
- A regular expression to check against
Return Value
1 if the regular expression regexp matches the string input, 0 if no match is found
StrRegExpFind
Find a substring using a regular expression.
<num> StrRegExpFind(<str> input, <str> regexp);
Parameters
- input
- String to find a substring in
- regexp
- Regular expression to find the substring
Return Value
Position in the string input where a match was found, or -1 if no match is found
Example
@var $str = "Program v3.10"; @var $len = StrRegExpFind( $str , ".v[0-9].[0-9][0-9]" ); // $len is 7
IsNumeric
Check whether a string value can be parsed as a number.
<num> IsNumeric( <str> input );
Parameters
- input
- A string to test
Return Value
1 if input represents a valid integer or floating-point number, 0 otherwise.
Example
@var $v1 = IsNumeric( "123" ); // 1 @var $v2 = IsNumeric( "3.14" ); // 1 @var $v3 = IsNumeric( "abc" ); // 0 @var $v4 = IsNumeric( "12x" ); // 0
StrStartsWith
Check whether a string starts with a given prefix (case-sensitive).
<num> StrStartsWith( <str> input, <str> prefix );
Parameters
- input
- The string to test
- prefix
- The prefix to look for
Return Value
1 if input begins with prefix, 0 otherwise.
Example
@var $r = StrStartsWith( "HelloWorld", "Hello" ); // 1 @var $r = StrStartsWith( "HelloWorld", "World" ); // 0
StrEndsWith
Check whether a string ends with a given suffix (case-sensitive).
<num> StrEndsWith( <str> input, <str> suffix );
Parameters
- input
- The string to test
- suffix
- The suffix to look for
Return Value
1 if input ends with suffix, 0 otherwise.
Example
@var $r = StrEndsWith( "report.txt", ".txt" ); // 1 @var $r = StrEndsWith( "report.txt", ".csv" ); // 0
StrContains
Check whether a string contains a given substring (case-sensitive).
<num> StrContains( <str> input, <str> sub );
Parameters
- input
- The string to search
- sub
- The substring to look for
Return Value
1 if sub is found anywhere in input, 0 otherwise.
Example
@var $r = StrContains( "Hello World", "World" ); // 1 @var $r = StrContains( "Hello World", "xyz" ); // 0
StrFormat
Build a formatted string using printf-style format specifiers.
<str> StrFormat( <str> format, <var> arg1 [, <var> arg2, ...] );
Parameters
- format
-
A format string containing literal text and format specifiers.
Common specifiers:
%s— string
%d— decimal integer
%f— floating-point number
%05d— zero-padded integer (width 5)
%.2f— float with 2 decimal places
%%— literal percent sign - arg1, arg2, …
- Values substituted into the format string in order
Return Value
A formatted string.
Example
@var $s = StrFormat( "Item %d of %d: %s", 3, 10, "report.txt" ); // $s == "Item 3 of 10: report.txt" @var $s = StrFormat( "Value: %05d", 42 ); // $s == "Value: 00042" @var $s = StrFormat( "Pi = %.4f", 3.14159 ); // $s == "Pi = 3.1416"
StrPadLeft
Pad a string on the left side to reach a minimum total width.
<str> StrPadLeft( <str> input, <num> width [, <str> padChar] );
Parameters
- input
- The string to pad
- width
- The minimum total length of the returned string
- padChar
- Optional. The character to pad with. Defaults to a space (
" "). Only the first character is used if a longer string is provided.
Return Value
input left-padded to at least width characters. If input is already at least width characters long, it is returned unchanged.
Example
@var $s = StrPadLeft( "42", 6 ); // " 42" @var $s = StrPadLeft( "42", 6, "0" ); // "000042" @var $s = StrPadLeft( "Hello", 3 ); // "Hello" (already wider)
StrPadRight
Pad a string on the right side to reach a minimum total width.
<str> StrPadRight( <str> input, <num> width [, <str> padChar] );
Parameters
- input
- The string to pad
- width
- The minimum total length of the returned string
- padChar
- Optional. The character to pad with. Defaults to a space (
" ").
Return Value
input right-padded to at least width characters. If input is already at least width characters long, it is returned unchanged.
Example
@var $s = StrPadRight( "Name", 10 ); // "Name " @var $s = StrPadRight( "Name", 10, "." ); // "Name......"
StrCount
Count the number of non-overlapping occurrences of a substring within a string (case-sensitive).
<num> StrCount( <str> input, <str> sub );
Parameters
- input
- The string to search
- sub
- The substring to count
Return Value
The number of times sub appears in input. Returns 0 if not found.
Example
@var $n = StrCount( "banana", "an" ); // 2 @var $n = StrCount( "Hello", "xyz" ); // 0
StrRepeat
Repeat a string a specified number of times.
<str> StrRepeat( <str> input, <num> count );
Parameters
- input
- The string to repeat
- count
- The number of times to repeat input. 0 returns an empty string.
Return Value
input concatenated count times.
Example
@var $s = StrRepeat( "ab", 3 ); // "ababab" @var $s = StrRepeat( "-", 20 ); // "--------------------" @var $s = StrRepeat( "x", 0 ); // ""