String Manipulation
A string stores a series of characters like "Let's cook!". The functions below can be used to manage these strings.
- addUnicodeEscapeChars
- converts unicode string to ascii format
- bytesToDescription
- string with a human readable description of size
- charAt
- Returns the character at the specified index (position)
- charCodeAt
- Returns the Unicode of the character at the specified index
- compareIgnoreCaseString
- Case-insensitive comparison with another string.
- containsString
- Tests whether the string contains another substring.
- dropLastCharsString
- Returns a version of this string with a number of characters removed from the end.
- endsWithIgnoreCaseString
- Tests whether the string ends with another string.
- endsWithString
- Tests whether the string ends with another string.
- equalsIgnoreCaseString
- Case-insensitive comparison with another string.
- findSubString
- finds a sub string within 'content', starting with 'from' and 'to' case-sensitive strings. 'from' and 'to' will be excluded from the result. (core 0.5.4)
- findSubStringBTF
- Finds a sub string (Back To Front) within 'content', starts searching from the beginning till 'to' and then finding the nearest 'from' seaching backwards. case-sensitive strings. 'from' and 'to' will be excluded from the result.
- fromCharCode
- Converts Unicode values to characters
- getLastCharFromString
- Returns a number of characters from the end of the string.
- getStringTokens
- creates an array with tokens from a given string
- hashCodeOfString
- Generates a probably-unique 64-bit hashcode from this string.
- isQuotedString
- Checks whether the string might be in quotation marks.
- objectToString
- converts an object to a json formatted string
- paddedLeftString
- Returns a copy of this string with the specified character repeatedly added to its beginning until the total length is at least the minimum length specified.
- paddedRightString
- Returns a copy of this string with the specified character repeatedly added to its end until the total length is at least the minimum length specified.
- quotedString
- Adds quotation marks around a string.
- replaceInString
- in the source string, replace any occurrence of the the search string with the replace string
- split
- Splits a string into an array of substrings
- startsWithIgnoreCaseString
- Tests whether the string begins with another string.
- startsWithString
- Tests whether the string begins with another string.
- stringContainsNonWhitespaceChars
- Returns true if this string contains any non-whitespace characters.
- stringToObject
- converts a json formatted string to an object
- substring
- returns the part of the string between the start and end indexes, or to the end of the string.
- toFixed
- creates a print friendly version of a floating point value by limiting decimals
- toHexString
- Returns a string containing a hex dump of a block of string data.
- toLowerCase
- converts a string to a lower case version
- toUpperCase
- converts a string to a upper case version
- trimEndString
- Returns a copy of this string with any whitespace characters removed from the end.
- trimStartString
- Returns a copy of this string with any whitespace characters removed from the start.
- trimString
- Returns a copy of this string with any whitespace characters removed from the start and end.
- unquotedString
- Removes quotation marks from around the string, (if there are any).
examples
// trim the decimals, to fix the floating point decimal display problem
function TestToFixed()
{
echo("toFixed " + toFixed(123.456789, 2));
echo("toFixed " + toFixed("789.123", 2));
}
// converts a JSON string to a object, adds a property value. and converts back to a string
function TestJSON()
{
var json_string = "{ \"prop1\" : \"value1\" }";
var json = stringToObject(json_string);
json["prop2"] = "value2";
echo(objectToString(json));
}
// create a readable string from a byte size integer
function TestBytesDescription()
{
echo(bytesToDescription(1));
echo(bytesToDescription(10));
echo(bytesToDescription(100));
echo(bytesToDescription(1000));
echo(bytesToDescription(10000));
echo(bytesToDescription(100000));
echo(bytesToDescription(1000000));
echo(bytesToDescription(10000000));
echo(bytesToDescription(100000000));
echo(bytesToDescription(1000000000));
echo(bytesToDescription(10000000000));
echo(bytesToDescription(100000000000));
}
function TestCases()
{
echo(toUpperCase("Hello"));
echo(toLowerCase("Hello"));
echo(containsString("test", "tes") ? "yes" : "no");
echo(trimString("\t\ntest trim1\t\n")); // prints "test"
echo(trimStartString("\t\ntest trim2\t\n")); // prints "test\t\n"
echo(trimEndString("\t\ntest trim3\t\n")); // prints "\t\ntest"
echo(unquotedString("\"quoted1\""));
echo(unquotedString("'quoted2'"));
echo(quotedString("quoted3"));
echo(isQuotedString(quotedString("quoted4")) ? "yes is quoted4" : " no isnt quoted");
echo(hashCodeOfString("some random string"));
echo(equalsIgnoreCaseString("bla bla", "Bla bLA") ? "equals ignore case" : "doesnt equals ignore case");
echo(compareIgnoreCaseString("8", "7") < 0 ? "acending or equal" : "decending");
echo(startsWithString("123456", "123") ? "starts with 123" : "doesnt start with 123");
echo(startsWithIgnoreCaseString("abcdef", "AbC") ? "starts with AbC" : "doesnt start with AbC");
echo(endsWithString("123456", "456") ? "ends with 456" : "doesnt end with 456");
echo(endsWithIgnoreCaseString("abcdef", "deF") ? "starts with AbC" : "doesnt start with AbC");
echo(stringContainsNonWhitespaceChars("\t\n") ? "containsNonWhitespaceChars" : "doesn't containsNonWhitespaceChars");
echo(toHexString("123456798", 1));
echo(dropLastCharsString("123456", 2));
echo(getLastCharFromString("123456", 2));
echo(paddedLeftString("123", "0", 6));
echo(paddedRightString("123", "0", 6));
}