Skip to content

StringUtils API

phoenixide edited this page Apr 6, 2024 · 2 revisions

utils/StringUtils

Utilities functions related to string manipulation

format

Format a string by replacing placeholder symbols with passed in arguments.

Example: var formatted = StringUtils.format("Hello {0}", "World");

Parameters

Returns string Formatted string

getLines

Splits the text by new line characters and returns an array of lines

Parameters

Returns Array<string> lines

offsetToLineNum

Returns a line number corresponding to an offset in some text. The text can be specified as a single string or as an array of strings that correspond to the lines of the string.

Specify the text in lines when repeatedly calling the function on the same text in a loop. Use getLines() to divide the text into lines, then repeatedly call this function to compute a line number from the offset.

Parameters

  • textOrLines (string | Array<string>) string or array of lines from which to compute the line number from the offset
  • offset number

Returns number line number

startsWith

Returns true if the given string starts with the given prefix.

Parameters

Returns Boolean

endsWith

Returns true if the given string ends with the given suffix.

Parameters

breakableUrl

Return an escaped path or URL string that can be broken near path separators.

Parameters

  • url string the path or URL to format

Returns string the formatted path or URL

prettyPrintBytes

Converts number of bytes into human readable format. If param bytes is negative it returns the number without any changes.

Parameters

  • bytes number Number of bytes to convert
  • precision number Number of digits after the decimal separator

Returns string

truncate

Truncate text to specified length.

Parameters

  • str string Text to be truncated.
  • len number Length to which text should be truncated

Returns string? Returns truncated text only if it was changed

hashCode

Computes a 32bit hash from the given string Taken from http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery

Parameters

  • str string The string for which hash is to be computed

Returns number The 32-bit hash

randomString

Generates a random nonce string of the specified length.

!!!Should not be used for crypto secure workflows.!!!

Parameters

  • stringLength number The length of the nonce in bytes. default 10. (optional, default 10)
  • prefix string? optional prefix (optional, default "")

Returns string The randomly generated nonce.