Utils
(utils)
Overview
The Utils
scripting object provides a range of utility methods for data transformation, formatting, and validation. It includes functionality for handling Base64 encoding and decoding, byte array manipulation, and string transformations such as escaping markup, formatting, and hashing. This makes it a versatile tool for managing data in different formats.
Key methods include base64ToBytes
, which converts Base64-encoded strings to byte arrays, and bytesToBase64
, which performs the reverse operation. The dateFormat
method allows dates to be formatted based on specified patterns, with support for custom timezones and locales. Similarly, numberFormat
enables precise number formatting, including locale-specific options.
Advanced string operations such as stringReplace
, stringFormat
, and stringTrim
are complemented by security-focused methods like stringMD5HashBase16
and stringPBKDF2Hash
, providing robust options for hashing and validation. For example, validatePBKDF2Hash
ensures that a given password matches a hash, supporting modern cryptographic standards like SHA256.
The object also facilitates Unicode character conversion, checks for related records using hasRecords
, and parses strings into date objects with parseDate
, offering rich functionality for varied use cases.
Methods Summarized
Return the byte array representation of the Base64 value
Return the String representation of the Base64 value
Return the Base64 value representation of the byteArray
Return the string conversion of the byteArray
Format a date object to a text representation.
Format a date object to a text representation using the format and timezone given.
Format a date object to a text representation.
Format a date object to a text representation using the format, timezone, language and country given.
Returns a string containing the character for the unicode number.
Returns true if the (related)foundset exists and has records.
Returns true if the (related)foundset exists and has records.
Returns true when Monday is the first day of the week for your current locale setting.
Format a number to have a defined fraction.
Format a number to have a defined fraction.
Format a number to specification.
Format a number to specification.
Parse a string to a date object.
Parse a string to a date object.
Parse a string to a date object.
Parse a string to a date object.
Returns the escaped markup text (HTML/XML).
Returns the escaped markup text (HTML/XML).
Returns the escaped markup text (HTML/XML).
Formats a string according to format specifiers and arguments.
Format a string using mask.
Replaces a portion of a string with replacement text from a specified index.
Returns all words starting with capital chars.
Returns a string with the requested number of characters, starting from the left.
Returns the number of words, starting from the left.
Returns the md5 hash (encoded as base16) for specified text.
Returns the md5 hash (encoded as base64) for specified text.
Returns a substring from the original string.
Returns a substring from the original string.
Returns the PBKDF2 hash for specified text.
Returns the PBKDF2 hash for specified text.
Returns the number of times searchString appears in textString.
Returns the position of the string to search for, from a certain start position and occurrence.
Replaces a portion of a string with replacement text.
Returns the text with %%tags%% replaced, based on provided record or foundset or form.
Returns a string with the requested number of characters, starting from the right.
Returns the number of words, starting from the right.
Return the Base64 representation of the string
Return the byte array representation of the string
Filters characters out of from a string and leaves digits, returns the number.
Filters characters out of from a string and leaves digits, returns the number.
Returns the string without leading or trailing spaces.
Returns the number of words in the text string.
Returns a datestamp from the timestamp (sets hours,minutes,seconds and milliseconds to 0).
Validates the given password against the given hash.
Methods Detailed
base64ToBytes(base64String:)
Return the byte array representation of the Base64 value
Parameters
Sample
base64ToString(base64String:)
Return the String representation of the Base64 value
Parameters
Sample
bytesToBase64(byteArray:)
Return the Base64 value representation of the byteArray
Parameters
Sample
bytesToHex(bytearray)
Parameters
Sample
bytesToString(byteArray:)
Return the string conversion of the byteArray
Parameters
Sample
dateFormat(date, format)
Parameters
Sample
dateFormat(date, format, timezone)
Parameters
Sample
dateFormat(date, format, language, country)
Parameters
Sample
dateFormat(date, format, timezone, language, country)
Parameters
Sample
getUnicodeCharacter(unicodeCharacterNumber)
Returns a string containing the character for the unicode number.
Parameters
Sample
hasRecords(foundset)
Returns true if the (related)foundset exists and has records. Another use is, to pass a record and qualified relations string to test multiple relations/foundset at once
Parameters
Sample
hasRecords(record, relationString)
Returns true if the (related)foundset exists and has records. Another use is, to pass a record and qualified relations string to test multiple relations/foundset at once
Parameters
Sample
hexToBytes(hex)
Parameters
Sample
hexToString(hex)
Parameters
Sample
isMondayFirstDayOfWeek()
Returns true when Monday is the first day of the week for your current locale setting.
Sample
numberFormat(number, digits)
Format a number to have a defined fraction.
Parameters
Sample
numberFormat(number, digits, language, country)
Format a number to have a defined fraction.
Parameters
Sample
numberFormat(number, format)
Format a number to specification.
Parameters
Sample
numberFormat(number, format, language, country)
Format a number to specification.
Parameters
Sample
parseDate(date, format)
Parse a string to a date object. This parses the date using the TimeZone of the server Format can be a string like: 'dd-MM-yyyy' , 'dd-MM-yyyy HH:mm' , 'MM/dd/yyyy', 'MM/dd/yyyy hh:mm aa', 'dd.MM.yyyy'
Parameters
Sample
parseDate(date, format, timezone)
Parse a string to a date object. Using the timezone that is given, if null then it formats it with the clients timezone.
see i18n.getAvailableTimeZoneIDs() to get a timezone string that can be used.
Format can be a string like: 'dd-MM-yyyy' , 'dd-MM-yyyy HH:mm' , 'MM/dd/yyyy', 'MM/dd/yyyy hh:mm aa', 'dd.MM.yyyy'
Parameters
Sample
parseDate(date, format, language, country)
Parse a string to a date object. Using language and country that are given, if null then it formats it with the locale of the client
Format can be a string like: 'dd-MM-yyyy' , 'dd-MM-yyyy HH:mm' , 'MM/dd/yyyy', 'MM/dd/yyyy hh:mm aa', 'dd.MM.yyyy'
Parameters
Sample
parseDate(date, format, timezone, language, country)
Parse a string to a date object. Using the timezone, language and country that are given, if null then it formats it with the timezone and locale of the client
see i18n.getAvailableTimeZoneIDs() to get a timezone string that can be used.
Format can be a string like: 'dd-MM-yyyy' , 'dd-MM-yyyy HH:mm' , 'MM/dd/yyyy', 'MM/dd/yyyy hh:mm aa', 'dd.MM.yyyy'
Parameters
Sample
stringEscapeMarkup(textString)
Returns the escaped markup text (HTML/XML).
Parameters
Sample
stringEscapeMarkup(textString, escapeSpaces)
Returns the escaped markup text (HTML/XML).
Parameters
Sample
stringEscapeMarkup(textString, escapeSpaces, convertToHtmlUnicodeEscapes)
Returns the escaped markup text (HTML/XML).
Parameters
Sample
stringFormat(text_to_format, parameters)
Formats a string according to format specifiers and arguments.
Parameters
Sample
stringFormat(text, format)
Format a string using mask.
Parameters
Sample
stringIndexReplace(text, i_start, i_size, replacement_text)
Replaces a portion of a string with replacement text from a specified index.
Parameters
Sample
stringInitCap(text)
Returns all words starting with capital chars.
Parameters
Sample
stringLeft(text, i_size)
Returns a string with the requested number of characters, starting from the left.
Parameters
Sample
stringLeftWords(text, numberof_words)
Returns the number of words, starting from the left.
Parameters
Sample
stringMD5HashBase16(textString)
Returns the md5 hash (encoded as base16) for specified text.
NOTE: MD5 (Message-Digest Algorythm 5) is a hash function with a 128-bit hash value, for more info see: http://en.wikipedia.org/wiki/MD5
Parameters
Sample
stringMD5HashBase64(textString)
Returns the md5 hash (encoded as base64) for specified text.
NOTE: MD5 (Message-Digest Algorythm 5) is a hash function with a 128-bit hash value, for more info see: http://en.wikipedia.org/wiki/MD5
Parameters
Sample
stringMiddle(text, i_start, i_size)
Returns a substring from the original string.
Parameters
Sample
stringMiddleWords(text, i_start, numberof_words)
Returns a substring from the original string.
Parameters
Sample
stringPBKDF2Hash(textString)
Returns the PBKDF2 hash for specified text. This method is preferred above the old MD5 hash for enhanced security. It uses a default of 9999 iterations. The string that is returned can only be used in the utils.validatePBKDF2Hash(password,thisReturnValue) to check if this hash is a result of that password. This will always be false: utils.stringPBKDF2Hash("test") == utils.stringPBKDF2Hash("test"). Because for the same string in multiply calls it will not generate the same hash. So you can only check it like this: utils.validatePBKDF2Hash("test",utils.stringPBKDF2Hash("test"))
NOTE: PBKDF2 is the key hash function for the PKCS (Public-Key Cryptography) standard, for more info see: http://en.wikipedia.org/wiki/PBKDF2
Parameters
Sample
stringPBKDF2Hash(textString, iterations)
Returns the PBKDF2 hash for specified text. This method is preferred above the old MD5 hash for enhanced security. From Servoy 2024.3.1 on this method will use SHA256 (HmacSHA256) as the Hash Algorithm. Before that it did use SHA1 (HmacSHA1) as the Hash Algorithm, this does result in the a different length of the hash. The total hash length (including interations of 9999) for SHA256 is 87 that was 63. So you need to make sure you can store at least 87 characters in your database. (but make sure you can handle more for future updates)
NOTE: PBKDF2 is the key hash function for the PKCS (Public-Key Cryptography) standard, for more info see: http://en.wikipedia.org/wiki/PBKDF2
Parameters
Sample
stringPatternCount(text, toSearchFor)
Returns the number of times searchString appears in textString.
Parameters
Sample
stringPosition(textString, toSearchFor, i_start, i_occurrence)
Returns the position of the string to search for, from a certain start position and occurrence.
Parameters
Sample
stringReplace(text, search_text, replacement_text)
Replaces a portion of a string with replacement text.
Parameters
Sample
stringReplaceTags(text, scriptable)
Returns the text with %%tags%% replaced, based on provided record or foundset or form.
Parameters
Sample
stringRight(text, i_size)
Returns a string with the requested number of characters, starting from the right.
Parameters
Sample
stringRightWords(text, numberof_words)
Returns the number of words, starting from the right.
Parameters
Sample
stringToBase64(string:)
Return the Base64 representation of the string
Parameters
Sample
stringToBytes(string:)
Return the byte array representation of the string
Parameters
Sample
stringToHex(string)
Parameters
Sample
stringToNumber(textString)
Filters characters out of from a string and leaves digits, returns the number. Uses locale decimal separator.
Parameters
Sample
stringToNumber(textString, decimalSeparator)
Filters characters out of from a string and leaves digits, returns the number. Decimal separator is specified as parameter.
Parameters
Sample
stringTrim(textString)
Returns the string without leading or trailing spaces.
Parameters
Sample
stringWordCount(text)
Returns the number of words in the text string.
Parameters
Sample
timestampToDate(date)
Returns a datestamp from the timestamp (sets hours,minutes,seconds and milliseconds to 0).
Parameters
Sample
validatePBKDF2Hash(password, hash)
Validates the given password against the given hash. The hash should be generated by one of the stringPBKDF2Hash(password [,iteration]) functions. If hash is null or empty string the method will return false. This method can handle the older generated hash values based on SHA1 (HmacSHA1) and the new SHA256 (HmacSHA256) hash values.
NOTE: PBKDF2 is the key hash function for the PKCS (Public-Key Cryptography) standard, for more info see: http://en.wikipedia.org/wiki/PBKDF2
Parameters
Sample
Last updated
Was this helpful?