Menu

String functions

Kalle

The following string functions are supported. The string functions are compatible with theire PHP pendants.
Note: String functions work byte orientated. UTF8 and Unicode characters can contain more than one byte. You have to be careful with fix positions when splitting a string. See also Datatypes.

Function/Arguments Return Description
strlen (string string) string Return the length of the given string in bytes.
strcmp (string string1, string string2) number Compare string1 and string2. Return values: 1 = string1 > string2, -1 = string1 < string2, 0 = string1 equal to string2
strpos (string searchIn, string seachFor) number Return the position of searchFor in seachIn as number beginning from 0. If searchFor was not found then false is returned.
substr (string string, number start [, number length]) string Return a part of a given string beginning from start with optional length. If length is not set then the end of the string will be returned. If start is negative then it counts from the end of the string.
strtoupper (string string) string Return a string in upper case. Only Ascii characters are converted to upper case.
strtolower (string string) string Return a string in lower case. Only Ascii characters are converted to lower case.
trim (string string [, string chars]) string Remove leading and endig spaces, line feeds and tabulators from string. If parameter chars is given then only the specified characters will be removed. The cleaned string will be returned.
rtrim (string string [, string chars]) string Same as trim() but only remove ending spaces from right side of the string.
ltrim (string string [, string chars]) string Same as trim() but only remove leading spaces from left side of the string.
str_replace (mixed searchFor, mixed replaceWith, string inString ) string Replace one ore more parts in a string. searchFor and replaceWith can be strings or arrays. For example: str_replace (array("programming", "I"), array ("coding", "You"), "I love V1 programming.") will replace multiple parts and produce the returned string "You love V1 coding".
explode (string delimiter, string string) array Return an array of splitted parts of a string. The string will be splitted in parts separated by the delimiter.
chr (number asciiNumber) string Return the character of a given asciiNumber. For example chr(32) will return space ' '.
utf8_encode (string string) string Convert Latin2 formatted string to UTF8 and return.
utf8_decode (string string) string Convert UTF8 formatted string to Latin2 and return.
bin2hex (string string) string Convert binary content of a string to hexadecimal format. For examle bin2hex ("V1") will return string "5631".
hex2bin (string string) string Convert hexadecimal string to binary string. For examle hex2bin ("5631") will return string "V1".
crc32 (string string) number Return the CRC32 checksum of a string.
resize (string& string, number length [,reserve=false, fillWith='']) bool Resize string buffer to length. If length is greater than the existing length, the complete string will be filled with zero bytes. If optional parameter reserve is true, only memory buffer will be reserved and nothing will be resized/overwritten. Optional paramer willWith can contain a string with length of 1 ascii character to fill the resized string instead of zero bytes. On success true is returned. Note: You can use this function to truncate strings, create empty strings with individual length or reserve memory buffer.
blank (string& string, [,zeroLength=false, fillWith='']) bool Blank out a string with zero bytes. If optional parameter zeroLength is true, the string will also resized to length of 0 after filling. Optional paramer willWith can contain a string with length of 1 ascii character to fill the string instead of zero bytes. On success true is returned. Note: You can use this function to blank out sensitive strings in memory.

The following functions can be used to parse and format binary data.

Function/Arguments Return Description
binparse (string string, [number format=7, number pos=0, bool signed=0, number nbo=0] ) number Parse a binary string and return the parsed number. format can be following: 1 = 8 Bit (char), 2 = 16 Bit (short), 3 = 32 Bit (int), 4 = 64 Bit (long long), 5 = 32 Bit (float), 6 = 64 Bit (double), 7 = integer with default width (defined in constant _ALIGN). Optional parameter pos set the position where parsing will start. If signed is true, then the number will returned signed, default is unsigned. If nbo is 1, then 32 Bit and 16 Bit will be converted from network byte order to host byte order. If nbo is 2, then 32 Bit and 16 Bit will be converted from host byte order to network byte order. If nbo is 0, then 32 Bit and 16 Bit are parsed in native machine format.
binformat (number number, [number format=7, number nbo=0] ) string Convert a number into a binary format and return the binary string. format can be following: 1 = 8 Bit (char), 2 = 16 Bit (short), 3 = 32 Bit (int), 4 = 64 Bit (long long), 5 = 32 Bit (float), 6 = 64 Bit (double), 7 = integer with default width (defined in constant _ALIGN). If optional parameter nbo is 1, then 32 Bit and 16 Bit will be converted from network byte order to host byte order. If nbo is 2, then 32 Bit and 16 Bit will be converted from host byte order to network byte order. If nbo is 0, then 32 Bit and 16 Bit are formatted in native machine format.

Note that 64 Bit integer numbers cannot be loaded completely into internal floating point representation. See datatypes.
To work with binary strings see also Native function calling.

back to Home


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.