Spoiler
Extended String Libraryv2.3
This library has been inspired by me trying to do some common high-level language string functions that don't exist in Lua. So at 2am, I decided to go through Java, C# and Objective-C string classes and implement most of the functions in Lua.
Some functions that are implemented are already in Lua, but I did them anyway. If there are any you wish to be added, if you find any bugs, or any incorrect implementations please report them here
Java Functions ( from here )
Spoiler
function charAt( str, index )
Returns the character at the specified index of the stringfunction compareTo( oneString, anotherString )
Compares two strings lexicographicallyfunction compareToIgnoreCase( oneString, anotherString )
Compares two strings lexicographically ignoring case differencesfunction concat( oneString, anotherString )
Returns a new string with the second string concatenated to the firstfunction contains( str, seq )
Returns true if, and only if, this string contains the specified sequence of character valuesfunction contentEquals( oneValue, anotherValue )
Compares the string against the specified valuefunction copyValueOf( tData )
Returns a String that represents the characters in the table specifiedfunction copySubsetOf( tData, offset, count )
Returns a String of the same length as count that represents the characters in the table specified starting from offsetfunction endsWith( str, suffix )
Tests if the string ends with the specified suffixfunction equals( str, value )
Compares the string to the specified valuefunction equalsIgnoreCase( oneStr, anotherStr )
Compares the string to another string, ignoring case considerationsfunction getBytes( str )
Encodes the string into a sequence of bytes, storing the result into a new byte arrayfunction getChars( str )
Converts this string to a new character table.function hashCode( str )
Returns a hash code for this string ( Java's algorithm )function indexOf( str, ch )
Returns the index within the string of the first occurrence of the specified characterfunction indexOfAfter( str, ch, index )
Returns the index within the string of the first occurrence of the specified character, starting the search at the specified index.function indexOfSubstring( str, sub )
Returns the index within the string of the first occurrence of the specified substring.function indexOfSubstringFrom( str, sub, index )
Returns the index within the string of the first occurrence of the specified substring, starting at the specified index.function isEmpty( str )
Returns true if, and only if, length is 0.function lastIndexOf( str, ch )
Returns the index within the string of the last occurrence of the specified character.function lastIndexOfFrom( str, ch, index )
Returns the index within the string of the last occurrence of the specified character, searching backward starting at the specified index.function lastIndexOfSubstring( str, sub )
Returns the index within the string of the rightmost occurrence of the specified substring.function lastIndexOfSubstringFrom( str, sub, index )
Returns the index within the string of the last occurrence of the specified substring, searching backward starting at the specified index.function regionMatches( str, sOffset, other, oOffset, len)
Tests if two string regions are equal.function replace( str, old, new )
Returns a new string resulting from replacing all occurrences of old in the string with newfunction replaceFirst( str, old, new )
Returns a new string resulting from replacing the first occurrence of old in the string with newfunction split( str, regex )
Returns table of the string split around matches of the given regular expressionfunction splitLimit( str, regex, lim )
Returns table of the string split around matches of the given regular expression, until limit - 1 is reachedfunction startsWith( str, prefix )
Tests if this string starts with the specified prefix.function subSequence( str, beginIndex, endIndex )
Returns a new character sequence that is a subsequence of the string.function toCharTable( str )
Converts this string to a new character table.function trim( str )
Returns a copy of the string, with leading and trailing whitespace omitted.function valueOf( tData )
Returns a string representation of the table suppliedfunction valueOfWith( tData, offset, count )
Returns a string representation of length count of the table supplied from offsetC# Functions ( from here )
Spoiler
function isWhitespace( str )
Indicates whether a specified string consists only of white-space characters.function padLeft( str, count )
Returns a new string that right-aligns the characters in this instance by padding them with spaces on the left, for a specified total length.function padLeftWith( str, count, char )
Returns a new string that right-aligns the characters in this instance by padding them on the left with a specified character, for a specified total length.function padRight( str, count )
Returns a new string that left-aligns the characters in this string by padding them with spaces on the right, for a specified total length.function padRightWith( str, count, char )
Returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.function trimEnd( str )
Removes all trailing occurrences of a set of characters specified in an array from the current Stringobject.function trimStart( str )
Removes all leading occurrences of a set of characters specified in an array from the current Stringobject.Objective-C Functions ( from here )
Spoiler
function stringWithContentsOfFile( path )
Returns a string with contents of the file at the given pathfunction stringWithContentsOfURL( url )
Returns a string with the contents of the given urlfunction writeToFile( str, path )
Writes the given string to a file at the given pathfunction characterAtIndex( str, index )
Returns the character at the given index of the stringfunction getCharacters( str, range )
Converts this string to a new character table.function componentsSeparatedByString( str, sub )
Returns a table containing substrings from the string that have been divided by a given separator.function substringFromIndex( str, index )
Returns a new string containing the characters of the string from the one at a given index to the end.function substringToIndex( str, index )
Returns a new string containing the characters of the string up to, but not including, the one at a given index.function substringWithRange( str, index, len )
Returns a string object containing the characters of the string that lie within a given range.function rangeOfString( str, sub )
Finds and returns the range of the first occurrence of a given string within the string.function rangeOfStringWithinRange( str, sub, index, len )
Finds and returns the range of the first occurrence of a given string, within the given range of the string.function stringByReplacingOccurrencesOfStringWithString( str, old, new )
Returns a new string in which all occurrences of a target string in the string are replaced by another given string.function capitalizedString( str )
Returns a capitalized representation of the string.function uppercaseString( str )
Returns an uppercased representation of the string.function lowercaseString( str )
Returns lowercased representation of the string.function intValue( str )
Returns a floored number of the given stringfunction boolValue( str )
Returns a boolean based on the string, if the string is not a boolean, returns nilfunction pathComponents( str )
Returns a table of strings containing, in order, each path component of the string.function lastPathComponent( str )
Returns the last path component of the string.function isAbsolutePath( str )
Returning a Boolean value that indicates whether the string represents an absolute path.function pathExtension( str )
Interprets the string as a path and returns the string's extension, if any.function stringByAppendingPathComponent( str, comp )
Returns a new string made by appending to the string a given string.function stringByAppendingPathExtension( str, ext )
Returns a new string made by appending to the string an extension separator followed by a given extension.function stringByDeletingLastPathComponent( str )
Returns a new string made by deleting the last path component from the string, along with any final path separator.function stringByDeletingPathExtension( str )
Returns a new string made by deleting the extension (if any, and only the last) from the string.function stringByAddingPercentEscapes( str )
Returns a representation of the string converted into a legal URL string.function stringByReplacingPercentEscapes( str )
Returns a new string made by replacing in the string all percent escapes with the matching charactersFunctions added by me, that I felt would be useful:
Spoiler
function containsIgnoreCase( str, seq )
Returns true if, and only if, this string contains the specified sequence of character values, ignoring case differencesfunction startsWithIgnoreCase( str, prefix )
Tests if this string starts with the specified prefix, ignoring case differencesfunction endsWithIgnoreCase( str, suffix )
Tests if the string ends with the specified suffix, ignoring case differencesfunction regionMatchesIgnoreCase( str, sOffset, other, oOffset, len)
Tests if two string regions are equal, ignoring case differencesfunction sentenceCase( str )
Returns a string with the first letter of each sentence capital and the rest lowercase.function titleCase( str )
Returns a string with the first letter of each word capital and the rest lowercase.function splitLineToTable( str, width )
Returns a table containing lines of text that do not exceed the length of the supplied width or a word being broken across two lines.function splitLine( str, width )
Returns a string containing line breaks at the given width without splitting a word over the linefunction count( str, regex )
Returns the number of times the regex appears in the stringfunction countIgnoreCase( str, regex )
Returns the number of times the regex appears in the string ignoring the casefunction isLower( str )
Returns a boolean representing if all characters in the string are lowercasefunction isUpper( str )
Returns a boolean representing if all characters in the string are uppercasefunction isAlpha( str )
Returns a boolean representing if all characters in the string are within the standard ASCII alphabetfunction isAlphaNumeric( str )
Returns a boolean representing if all characters in the string are either within the standard ASCII alphabet or a numberfunction isNumeric( str )
Returns a boolean representing if all characters in the string are numbersfunction isPunctuation( str )
Returns a boolean representing if all character in the string are punctuationRequested
Spoiler
Requested by GravityScore:function isHexadecimal( str )
Returns a boolean representing if all characters in the string are hexadecimal ( NOTE: Cannot support 0x20 formatted hexadecimal )Using the OO (Object-Oriented) Library
Spoiler
creating an extended string objectlocal output = str:new( "Yes this is string!" )
interacting with an extended string object
output:charAt( 1 )
this can be done with any function**the following functions are not OO and should be called like normal
- copyValueOf( tData )
- copySubsetOf( tData, offset, count )
- valueOf( tData )
- valueOfWith( tData, offset, count )
- stringWithContentsOfFile( path )
- stringWithContentsOfURL( url )
Download the standard library here
Download the String Object library here
Bug and Suggestion Reporter here
Change log:
Spoiler
v2.3- FIXED: isLower, isUpper, isAlpha, isAlphaNumeric — all now use pattern matching
- ADDED: isNumeric, isPunctuation, isHexadecimal
- ADDED: isLower, isUpper, isAlpha, isAlphaNumeric
- I can't remember… oops…
- ADDED: New download, string object library - deal with this api as an object
- FIXED: General bugfix
- ADDED: 2 new functions
- ADDED: 2 new functions
- ADDED: Implemented a selection of Objective-C methods
- FIXED: Misc bugfixes
- ADDED: Implemented a selection of C# methods
- FIXED: Misc bugfixes
- FIXED: Bug in compareTo and compareToIgnoreCase functions, weren't comparing lexicographically
- ADDED: hashCode function to Java's implementation
- FIXED: bug in isEmpty now returns true if length = 0
- Initial release