Options
All
  • Public
  • Public/Protected
  • All
Menu

A string builder class.

example
import { StringBuilder } from "@marcelkloubert/strings"

const str = new StringBuilder("Foo") // "Foo" && str.isEmpty === false
.append("bar") // "Foobar"
.prepend("buzz") // "buzzFoobar"
.clear() // str.length === 0

str.appendFormat("{0}, {1:upper,trim}", " Marcel ", "Kloubert") // "Kloubert, MARCEL"
str.length = 0 // "" && str.isEmpty === true

Hierarchy

  • StringBuilder

Index

Constructors

Properties

value: string

The current value.

newLine: string = "\n"

The default value for a new line.

Accessors

  • get isEmpty(): boolean
  • Gets if the value is empty or not.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    const str = new StringBuilder() // str.isEmpty === true

    str.append("Foo") // str.isEmpty === true

    str.length = 0 // str.isEmpty === true

    Returns boolean

  • get length(): number
  • set length(newLength: number): void
  • Gets or sets the length.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    const str = new StringBuilder() // str.length === 0

    str.append("Foo") // str.length === 3

    str.length = 1 // "Fo"
    str.length = 0 // ""

    Returns number

  • Gets or sets the length.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    const str = new StringBuilder() // str.length === 0

    str.append("Foo") // str.length === 3

    str.length = 1 // "Fo"
    str.length = 0 // ""

    Parameters

    • newLength: number

    Returns void

Methods

  • Appends the string representation of a value to the current value.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder()
    .append("foo") // "foo"
    .append("Bar") // "fooBar"

    Parameters

    • value: any

      The value to append.

    Returns StringBuilder

  • Appends a formatted string.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder()
    .appendFormat("foo: {0}", 5979) // "foo: 5979"
    .appendFormat(" {0:lower} {1:upper,trim}", "BAR", " Buzz ") // "foo: 5979 bar BUZZ"

    Parameters

    • formatStr: string

      The format string.

    • Rest ...args: any[]

    Returns StringBuilder

  • appendFormatArray(formatStr: string, argList: List<any>): StringBuilder
  • Appends a formatted string.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder()
    .appendFormatArray("foo: {0}", [5979]) // "foo: 5979"
    .appendFormatArray(" {0:lower} {1:upper,trim}", ["BAR", " Buzz "]) // "foo: 5979 bar BUZZ"

    Parameters

    • formatStr: string

      The format string.

    • argList: List<any>

      One or more argument for the format string.

    Returns StringBuilder

  • Joins a list of values to one string and appends it.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder("foo")
    .appendJoin(" + ", "Bar", "Baz") // "fooBar + Baz"

    Parameters

    • separator: string

      The separator.

    • Rest ...args: any[]

    Returns StringBuilder

  • appendJoinArray(separator: string, argList: List<any>): StringBuilder
  • Joins a list of values to one string and appends it.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder("foo")
    .appendJoinArray(" + ", ["Bar", "Baz"]) // "fooBar + Baz"

    Parameters

    • separator: string

      The separator.

    • argList: List<any>

      One or more argument for the format string.

    Returns StringBuilder

  • Appends the string representation of a value to the current value and adds a new line.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder()
    .appendLine("foo") // "foo\n"
    .appendLine("Bar") // "foo\nBar\n"

    Parameters

    • value: any

      The value to append.

    Returns StringBuilder

  • Clears the underlying value.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder()
    .append("foo") // "foo"
    .clear() // ""

    Returns StringBuilder

  • Clones that instance.

    import { StringBuilder } from "@marcelkloubert/strings"

    const str1 = new StringBuilder()
    const str2 = str1.clone()

    str1 === str2 // (false)
    str1.equals(str2) // (true)

    Returns StringBuilder

    The cloned instance.

  • equals(other: any): boolean
  • Handles another value as string and checks if it is equal qith this instance.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    const str = new StringBuilder()

    str.equals("") // (true)
    str.equals(null) // (true)
    str.equals(undefined) // (true)

    str.append("123")
    str.equals(123) // (true)

    Parameters

    • other: any

      The other value.

    Returns boolean

    Both values are equal.

  • getNewLine(): string
  • Insert a value to this string.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder("0123456789")
    .remove(3, 4) // "012789"

    Parameters

    • start: number

      The zero based start index.

    • value: any

      The value to insert.

    Returns StringBuilder

  • Prepends the string representation of a value to the current value.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder()
    .prepend("Foo") // "Foo"
    .prepend("bar") // "barFoo"

    Parameters

    • value: any

      The value to append.

    Returns StringBuilder

  • Appends a formatted string.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder()
    .prependFormat("foo: {0}", 5979) // "foo: 5979"
    .prependFormat("{0:lower} {1:upper,trim} ", "BAR", " Buzz ") // "bar BUZZ foo: 5979"

    Parameters

    • formatStr: string

      The format string.

    • Rest ...args: any[]

    Returns StringBuilder

  • prependFormatArray(formatStr: string, argList: List<any>): StringBuilder
  • Prepends a formatted string.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder()
    .prependFormatArray("foo: {0}", [5979]) // "foo: 5979"
    .prependFormatArray("{0:lower} {1:upper,trim} ", ["BAR", " Buzz "]) // "bar BUZZ foo: 5979"

    Parameters

    • formatStr: string

      The format string.

    • argList: List<any>

      One or more argument for the format string.

    Returns StringBuilder

  • Joins a list of values to one string and prepends it.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder("foo")
    .prependJoin(" + ", "Bar", "Baz") // "Bar + Bazfoo"

    Parameters

    • separator: string

      The separator.

    • Rest ...args: any[]

    Returns StringBuilder

  • prependJoinArray(separator: string, argList: List<any>): StringBuilder
  • Joins a list of values to one string and prepends it.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder("foo")
    .prependJoinArray(" + ", ["Bar", "Baz"]) // "Bar + Bazfoo"

    Parameters

    • separator: string

      The separator.

    • argList: List<any>

      One or more argument for the format string.

    Returns StringBuilder

  • Prepends the string representation of a value to the current value and adds a new line.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder()
    .append("Bar") // "Bar"
    .prependLine("foo") // "foo\nBar"

    Parameters

    • value: any

      The value to append.

    Returns StringBuilder

  • Removes a part from the current string.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder("0123456789")
    .remove(3, 4) // "012789"

    Parameters

    • start: number

      The zero based start index.

    • length: number

      The length.

    Returns StringBuilder

  • replace(searchFor: string | RegExp, replaceWith: any): StringBuilder
  • Replaces a substring with a new one.

    example
    import { StringBuilder } from "@marcelkloubert/strings"

    new StringBuilder("Foo BAZZ bar")
    .replace(" BAZZ ", "+++ baz +++") // "Foo+++ baz +++bar"
    .replace(/(foo)/i, "NewFoo") // "NewFoo+++ baz +++bar"

    Parameters

    • searchFor: string | RegExp

      The value or refular expression to search for.

    • replaceWith: any

      The new value.

    Returns StringBuilder

  • toJSON(): any
  • toString(): string

Generated using TypeDoc