Function set "String functions"

Functions manipulating strings

Summary

strBack()Returns the first character in a string.
strBackFind()Finds a substring bakwards.
strBackTrim()Removes white spaces at both the beginning and the end of the string.
strBuffer()Pre-allocates an empty string.
strCmpIgnoreCase()Performs a lexicographic comparation of two strings, ignoring character case.
strEndsWith()Check if a strings ends with a substring.
strFill()Fills a string with a given character or substring.
strFind()Finds a substring.
strFromMemBuf()Convets a MemBuf to a string.
strFront()Returns the first character in a string.
strFrontTrim()Removes white spaces from the front of the string.
strLower()Returns a lowercase version of the given string.
strMerge()Merges an array of strings into a string.
strReplace()Replaces the all the occurrences of a substring with another one.
strReplicate()Returns a new string that is created by replicating the original one.
strSplit()Subdivides a string in an array of substrings given a token substring.
strSplitTrimmed()Subdivides a string in an array of substrings given a token substring.
strStartsWith()Check if a strings starts with a substring.
strToMemBuf()Convets a string into a Memory Buffer
strTrim()Removes the white spaces at the beginning and at the end of a string.
strUpper()Returns an upper case version of the string.
strWildcardMatch()Perform an old-style file-like jolly-based wildcard match.

Members

strBack()

Returns the first character in a string.

strBack( str, [count], [remove], [numeric] )

strThe string on which to operate.
countNumber of characters to be taken (defaults to 1).
removeIf true, remove also the character.
numericIf true, returns a character value instead of a string.
Returns:The 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 character is removed and the string is shrinked.

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.

strBackFind()

Finds a substring bakwards.

strBackFind( string, needle, [start], [end] )

stringString where the search will take place.
needleSubstring to search for.
startOptional position from which to start the search in string.
endOptional position at which to end the search in string.
Returns:The 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] )

stringThe string to be trimmed.
trimSetA set of characters that must be removed.
Returns:The 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 )

sizeSize of the pre-allocated string.
Returns:The 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 )

string1First string to be compared.
string2Second string to be compared.
Returns:-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] )

stringThe string that is going to be tested for the given token.
tokenThe substring that will be compared with this string.
icaseIf true, pefroms a case neutral check
Returns: True 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.

strFill()

Fills a string with a given character or substring.

strFill( string, chr )

stringThe string to be filled.
chr The character (unicode value) or substring used to refill the string.
Returns:The 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] )

stringString where the search will take place.
needleSubstring to search for.
startOptional position from which to start the search in string.
endOptional position at which to end the search in string.
Returns:The 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 )

membufA MemBuf that will be converted to a string.
Returns:The 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] )

strThe string on which to operate.
countNumber of characters to be taken (defaults to 1).
removeIf true, remove also the character.
numericIf true, returns a character value instead of a string.
Returns:The 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 shrinked.

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.

strFrontTrim()

Removes white spaces from the front of the string.

strFrontTrim( string, [trimSet] )

stringThe string to be trimmed.
trimSetA set of characters that must be removed.
Returns:The 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 )

stringString that must be lowercased.
Returns:The 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] )

arrayAn array of strings to be merged.
mergeStrA string placed between each merge.
countMaximum count of merges.
Returns:The 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] )

stringThe string where the replace will take place.
substrThe substring that will be replaced.
repstrThe string that will take the place of substr.
startOptional start position in the string.
endOptional end position in the string.
Returns:A 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 )

stringThe string to be replicaeted.
timesNumber of times the string must be replicated.
Returns:The 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] )

stringThe string that must be splitted
tokenThe token by which the string should be splitted.
countOptional maximum split count.
Returns:An array of strings containing the splitted 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.

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] )

stringThe string that must be splitted
tokenThe token by which the string should be splitted.
countOptional maximum split count.
Returns:An array of strings containing the splitted 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" adjiacent token. 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] )

stringThe string that is going to be tested for the given token.
tokenThe substring that will be compared with this string.
icaseIf true, pefroms a case neutral check
Returns: True 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] )

stringString to be converted in a membuf.
wordWidthThe memory buffer word width (defaults to string character size).
Returns:The 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] )

stringThe string to be trimmed.
trimSetA set of characters that must be removed.
Returns:The 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.

strUpper()

Returns an upper case version of the string.

strUpper( string )

stringString that must be uppercased.
Returns:The 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] )

stringString that must match the wildcard.
wildcardA wildcard, possibly but not necessarily including a jolly character.
ignoreCaseIf true, the latin 26 base letters case is ignored in matches.
Returns:True 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:


Made with faldoc 2.1.0