1.18String functions

Functions manipulating strings

Functions

strBack

Returns the last character(s) in a string.

strBack( str, [count],[remove],[numeric] )
str The string on which to operate.
count Number of characters to be taken (defaults to 1).
remove If true, remove also the character.
numeric If true, returns a character value instead of a string.
ReturnThe first element or nil if the string is empty.

This function returns a string containing one character from the end of str, or eventually more characters in case a number > 1 is specified in count.

If remove is true, then the characters are removed and the string is shrunk.

If numeric is true, the UNICODE value of the string character will be returned, otherwise the caller will receive a string containing the desired character. In this case, count parameter will be ignored and only one UNICODE value will be returned.

See also: String.

strBackFind

Finds a substring backwards.

strBackFind( string, needle, [start],[end] )
string String where the search will take place.
needle Substring to search for.
start Optional position from which to start the search in string.
end Optional position at which to end the search in string.
ReturnThe position where the substring is found, or -1.

Works exactly as strFind, except for the fact that the last match in the string (or in the specified interval) is returned.

strBackTrim

Removes white spaces at both the beginning and the end of the string.

strBackTrim( string, [trimSet] )
string The string to be trimmed.
trimSet A set of characters that must be removed.
ReturnThe trimmed substring.

A new string, which is a copy of the original one with all characters in trimSet at the beginning and at the end of the string removed, is returned. If trimSet is not supplied, it defaults to space, tabulation characters, new lines and carriage returns. The original string is unmodified.

strBuffer

Pre-allocates an empty string.

strBuffer( size )
size Size of the pre-allocated string.
ReturnThe new string.

The returned string is an empty string, and equals to "". However, the required size is pre-allocated, and addition to this string (i.e. += operators) takes place in a fraction of the time otherwise required, up tho the filling of the pre-allocated buffer. Also, this string can be fed into file functions, the pre-allocation size being used as the input read size.

strCmpIgnoreCase

Performs a lexicographic comparation of two strings, ignoring character case.

strCmpIgnoreCase( string1, string2 )
string1 First string to be compared.
string2 Second string to be compared.
Return-1, 0 or 1.

The two strings are compared ignoring the case of latin characters contained in the strings.

If the first string is greater than the second, the function returns a number less than 0. If it's smaller, it returns a positive number. If the two strings are the same, ignoring the case of the characters, 0 is returned.

strEndsWith

Check if a strings ends with a substring.

strEndsWith( string, token, [icase] )
string The string that is going to be tested for the given token.
token The substring that will be compared with this string.
icase If true, pefroms a case neutral check
ReturnTrue if token matches the end of string, false otherwise.

This functioin performs a comparation check at the end of the string. If this string ends with token, the function returns true. If token is larger than the string, the function will always return false, and if token is an empty string, it will always match.

The optional parameter icase can be provided as true to have this function to perform a case insensitive match.

strEscape

Escape quotes and special characters in the string

strEscape( string, [full] )
string the String to be escaped.
full If true, characters above UNICODE 127 are escaped as well.
ReturnA new escaped string.

See also: String, strUnesq.

strEsq

Escape quotes in the given string.

strEsq( string, [inplace] )
string the String to be escaped.
inplace if true, the source string is modified, saving memory.
ReturnA new escaped string, if inplace is not given, or the string parameter if inplace is true.

See also: String, strUnesq.

strFill

Fills a string with a given character or substring.

strFill( string, chr )
string The string to be filled.
chr The character (unicode value) or substring used to refill the string.
ReturnThe string itself.

This function fills the physical storage of the given string with a single character or a repeated substring. This can be useful to clean a string used repeatedly as input buffer.

The function returns the same string that has been passed as the parameter.

strFind

Finds a substring.

strFind( string, needle, [start],[end] )
string String where the search will take place.
needle Substring to search for.
start Optional position from which to start the search in string.
end Optional position at which to end the search in string.
ReturnThe position where the substring is found, or -1.

Returns the index in string were needle begins, or -1 if not present. Giving a start parameter will cause the search to start from the given position up to the end of the string; if a match can be made at start position, then the the returned value will be the same as start, so when repeating searches in a string for all the possible matches, repeat until the result is -1 by adding one to the returned value and using it as start position for the next search.

If an end position is given, it is used as upper limit for the search, so that the search is in the interval [start, end-1].

Note: This function is equivalent to the fbom method String.find

strFromMemBuf

Convets a MemBuf to a string.

strFromMemBuf( membuf )
membuf A MemBuf that will be converted to a string.
ReturnThe resulting string.

This string takes each element of the membuf and converts it into a character in the final string. The contents of the buffer are not transcoded. It is appropriate to say that this function considers each element in the MemBuf as an Unicode value for the character in the final string.

To create a string from a buffer that may come from an encoded source (i.e. a file), use directly Transcode functions.

strFront

Returns the first character in a string.

strFront( str, [count],[remove],[numeric] )
str The string on which to operate.
count Number of characters to be taken (defaults to 1).
remove If true, remove also the character.
numeric If true, returns a character value instead of a string.
ReturnThe first element or nil if the string is empty.

This function returns a string containing one character from the beginning of str, or eventually more characters in case a number > 1 is specified in count.

If remove is true, then the character is removed and the string is shrunk.

If numeric is true, the UNICODE value of the string character will be returned, otherwise the caller will receive a string containing the desired character. In this case, count parameter will be ignored and only one UNICODE value will be returned.

See also: String.

strFrontTrim

Removes white spaces from the front of the string.

strFrontTrim( string, [trimSet] )
string The string to be trimmed.
trimSet A set of characters that must be removed.
ReturnThe trimmed substring.

A new string, which is a copy of the original one with all characters in trimSet at the beginning of the string removed, is returned. If trimSet is not supplied, it defaults to space, tabulation characters, new lines and carriage returns. The original string is unmodified.

strLower

Returns a lowercase version of the given string.

strLower( string )
string String that must be lowercased.
ReturnThe lowercased string.

All the Latin characters in the string are turned lowercase. Other characters are left untouched.

strMerge

Merges an array of strings into a string.

strMerge( array, [mergeStr],[count] )
array An array of strings to be merged.
mergeStr A string placed between each merge.
count Maximum count of merges.
ReturnThe merged string.

The function will return a new string containing the concatenation of the strings inside the array parameter. If the array is empty, an empty string is returned. If a mergeStr parameter is given, it is added to each pair of string merged; mergeStr is never added at the end of the new string. A count parameter may be specified to limit the number of elements merged in the array.

The function may be used in this way:


   a = strMerge( [ "a", "string", "of", "words" ], " " )
   printl( a ) // prints "a string of words"

If an element of the array is not a string, an error is raised.

strReplace

Replaces the all the occurrences of a substring with another one.

strReplace( string, substr, repstr, [start],[end] )
string The string where the replace will take place.
substr The substring that will be replaced.
repstr The string that will take the place of substr.
start Optional start position in the string.
end Optional end position in the string.
ReturnA copy of the string with the occourences of the searched substring replaced.

This is a flexible function that allows to alter a string by changing all the occurrences of a substring into another one. If the start parameter is given, the search and replacement will take place only starting at the specified position up to the end of the string, and if the end parameter is also provided, the search will take place in the interval [start, end-1].

strReplicate

Returns a new string that is created by replicating the original one.

strReplicate( string, times )
string The string to be replicaeted.
times Number of times the string must be replicated.
ReturnThe new string.

A nice shortcut. Also, this function performs the work efficiently, preallocating the needed space in one shot and avoiding the need to grow the string while replicating the original value.

strSplit

Subdivides a string in an array of substrings given a token substring.

strSplit( string, [token],[count] )
string The string that must be split.
token The token by which the string should be split.
count Optional maximum split count.
ReturnAn array of strings containing the split string.

This function returns an array of strings extracted from the given parameter. The array is filled with strings extracted from the first parameter, by dividing it based on the occurrences of the token substring. A count parameter may be provided to limit the splitting, so to take into consideration only the first relevant tokens. Adjacent matching tokens will cause the returned array to contains empty strings. If no matches are possible, the returned array contains at worst a single element containing a copy of the whole string passed as a parameter.

For example, the following may be useful to parse a INI file where keys are separated from values by "=" signs:


   key, value = strSplit( string, "=", 2 )

This code would return an array of 2 items at maximum; if no "=" signs are found in string, the above code would throw an error because of unpacking size, a thing that may be desirable in a parsing code. If there are more than one "=" in the string, only the first starting from left is considered, while the others are returned in the second item, unparsed.

If the token is empty or not given, the string is returned as a sequence of 1-character strings in an array.

Note: This function is equivalent to the fbom method String.split. The above example can be rewritten as:


   key, value = string.split( "=", 2 )

strSplitTrimmed

Subdivides a string in an array of substrings given a token substring.

strSplitTrimmed( string, token, [count] )
string The string that must be split.
token The token by which the string should be split.
count Optional maximum split count.
ReturnAn array of strings containing the split string.

This function returns an array of strings extracted from the given parameter. The array is filled with strings extracted from the first parameter, by dividing it based on the occurrences of the token substring. A count parameter may be provided to limit the splitting, so to take into consideration only the first relevant tokens. Adjacent matching tokens will be ignored. If no matches are possible, the returned array contains at worst a single element containing a copy of the whole string passed as a parameter.

Contrarily to strSplit, this function will "eat up" adjacent tokens. While strSplit is more adequate to parse field-oriented strings (as i.e. colon separated fields in configuration files) this function is best employed in word extraction.

Note: this function is equivalent to the FBOM method String.splittr

Note: See Tokenizer for a more adequate function to scan extensively wide strings.

strStartsWith

Check if a strings starts with a substring.

strStartsWith( string, token, [icase] )
string The string that is going to be tested for the given token.
token The substring that will be compared with this string.
icase If true, pefroms a case neutral check
ReturnTrue if token matches the beginning of string, false otherwise.

This functioin performs a comparation check at the beginning of the string. If this string starts with token, the function returns true. If token is larger than the string, the function will always return false, and if token is an empty string, it will always match.

The optional parameter icase can be provided as true to have this function to perform a case insensitive match.

strToMemBuf

Convets a string into a Memory Buffer

strToMemBuf( string, [wordWidth] )
string String to be converted in a membuf.
wordWidth The memory buffer word width (defaults to string character size).
ReturnThe resulting membuf.

This function creates a membuf from a string. The resulting membuf has the same word width of the original string, which may be 1, 2 or 4 byte wide depending on the size needed to store its contents. It is possible to specify a different word width; in that case the function will be much less efficient (each character must be copied).

If wordWidth is set to zero, the resulting memory buffer will have 1 byte long elements, but the content of the string will be copied as-is, bytewise, regardless of its character size.

strTrim

Removes the white spaces at the beginning and at the end of a string.

strTrim( string, [trimSet] )
string The string to be trimmed.
trimSet A set of characters that must be removed.
ReturnThe trimmed substring.

A new string, which is a copy of the original one with all characters in trimSet at the end of the string removed, is returned. If trimSet is not supplied, it defaults to space, tabulation characters, new lines and carriage returns. The original string is unmodified.

strUnescape

Unescape quotes and special characters in the string

strUnescape( string, [inplace] )
string the String to be escaped.
inplace if true, the source string is modified, saving memory.
ReturnA new unescaped string, if inplace is not given, or the string parameter if inplace is true.

See also: String, strUnesq, String.

strUnesq

Unescape the quotes in given string.

strUnesq( string, [inplace] )
string the String to be unescaped.
inplace if true, the source string is modified, saving memory.
ReturnA new unescaped string, if inplace is not given, or the string parameter if inplace is true.

This function transforms all the occourences of '\"' and '\'' into a double or single quote, leaving all the other special escape sequences untouched.

See also: String, strEsq.

strUpper

Returns an upper case version of the string.

strUpper( string )
string String that must be uppercased.
ReturnThe uppercased string.

All the Latin characters in the string are turned uppercase. Other characters are left untouched.

strWildcardMatch

Perform an old-style file-like jolly-based wildcard match.

strWildcardMatch( string, wildcard, [ignoreCase] )
string String that must match the wildcard.
wildcard A wildcard, possibly but not necessarily including a jolly character.
ignoreCase If true, the latin 26 base letters case is ignored in matches.
ReturnTrue if the string matches, false otherwise.

This function matches a wildcard that may contain jolly "*" or "?" characters against a string, eventually ignoring the case of the characters. This is a practical function to match file names against given patterns. A "?" in the wildcard represents any single character, while a "*" represents an arbitrary sequence of characters.

The wildcard must match completely the given string for the function to return true.

For example:

See also: String.

See also: String.

Made with http://www.falconpl.org