Strings

object Strings

Strings provides various string utilities.

Author

Todd L Smith

Properties

Link copied to clipboard

A regex Pattern containing just a line break.

Functions

Link copied to clipboard
fun addLineNumbers(source: String, pattern: String, startingLineNumber: Int): String

Add line numbers to the given string. Start the numbering at the specified value.

Link copied to clipboard
fun buildUnicodeBox(title: String = "", borderColumns: Int = 80, builder: StringBuilder.() -> Unit): String

Build a Unicode box whose contents are populated by the specified builder function. Only top and bottom borders are drawn.

Link copied to clipboard

Produce an escaped variant of the specified string.

Link copied to clipboard

Convert a string into HTML-escaped form, allowing the original string to be presented literally in a web browser or Swing component expecting HTML text.

Link copied to clipboard
fun increaseIndentation(originalString: String, increasedIndentation: Int): String

Increase the indentation by the given non-negative amount. The first line of the string (prior to the first line break) is not affected.

Link copied to clipboard
fun String.matchesAbbreviation(abbreviation: String, allowPartials: Boolean = false, ignoreCase: Boolean = false): List<Pair<Int, Int>>

Find the characters of abbreviation within the receiver, in the order specified by abbreviation but not necessarily contiguously.

Link copied to clipboard

Append a newline ('\n' = U+000A) then the specified number of tab ('\t' = U+0009) characters to the given StringBuilder.

Link copied to clipboard
fun repeated(string: String, count: Int): String

Answer a String consisting of count repetitions of string, concatenated.

Link copied to clipboard
fun StringBuilder.tab(indent: Int)

Append the specified number of tab ('\t') characters to the receiver, a StringBuilder.

Link copied to clipboard
fun tabs(indent: Int): String

Answer a String containing the specified number of tabs.

Link copied to clipboard
fun StringBuilder.tag(tag: String, vararg attributes: Pair<String, String>, body: StringBuilder.() -> Unit)

Output an XML tag with the given tag name and attributes supplied as Pairs. The values of the attributes will be escaped and quoted. Then run the body function and output a matching close tag. If at exception is thrown by the body, still attempt to output the close tag.

Link copied to clipboard

Answer the stringification of the stack for the specified exception.

Link copied to clipboard
fun String.truncateTo(limit: Int, ellipsis: String = "…"): String

If the receiver is more than limit characters, truncate it and append the ellipsis, ensuring the resulting string does not exceed the limit. The limit must be at least the length of the ellipsis.

Link copied to clipboard
fun StringBuilder.wrap(prefix: String, suffix: String, body: StringBuilder.() -> Unit)

With the given StringBuilder, append the prefix, run the body, and append the suffix. Attempt to append the suffix even if the body fails.