-
You are here: JS-methods > Documentation > String

String prototype extensions

Extends string prototype with the following methods: camelize, capitalize, collapseSpaces, format, ltrim, pad, remove, repeat, reverse, rtrim, sprintf, stripTags, trim, truncate

This extensions doesn't depend on any other code or overwrite existing methods.

  • camelize() : String
    Converts a string separated by dashes and/or underscores into a camelCase equivalent. For instance, 'java-script' would be converted to 'javaScript'.

    Example:

    "java-script".camelize();

    Result:

    "javaScript"

    Example:

    "java_script_methods".camelize();

    Result:

    "javaScriptMethods"

    Example:

    "java_script-methods".camelize();

    Result:

    "javaScriptMethods"
  • capitalize() : String
    Capitalizes the first letter of a string and downcases all the others.

    Example:

    "my name is harry".capitalize();

    Result:

    "My Name Is Harry"
  • collapseSpaces() : String
    Entfernt Whitespace am Anfang und Ende des Strings und reduziert Whitespace innerhalb des Strings auf ein Zeichen.

    Example:

    " My   name      is  Harry!   ".collapseSpaces();

    Result:

    "My name is Harry!"
  • format(Array<String> substrings) : String
    Substitute substrings from an array into a format String that includes '%1'-type specifiers

    Example:

    "My %0 is %1".format(['name','Harry']);

    Result:

    "My name is Harry"
  • ltrim() : String
    Removes leading whitespace.

    Example:

    "  My name is Harry!".ltrim();

    Result:

    "My name is Harry!"
  • pad(Int length, String ch, Int direction) : String
    Pad a string up to a given length. Padding characters are added to the left of the string.

    Example:

    "22".pad(4, '#');

    Result:

    "##22"

    Example:

    "22".pad(4, '#', 1);

    Result:

    "22##"
  • remove(Int start, Int length) : String
    Removes a specified length of characters from a given postion.

    Example:

    "JavaScript".remove(4, 6);

    Result:

    "Java"
  • repeat(Int count, String separator) : String
    Returns 'str' repeated 'count' times, optionally placing 'separator' between each repetition

    Example:

    "Hello".repeat(5);

    Result:

    "HelloHelloHelloHelloHello"

    Example:

    "Hello".repeat(5,'#');

    Result:

    "Hello#Hello#Hello#Hello#Hello"
  • reverse() : String
    Reverses the characters in the specified string.

    Example:

    "Hello".reverse();

    Result:

    "olleH"
  • rtrim() : String
    Removes trailing whitespace.

    Example:

    "My name is Harry!   ".rtrim();

    Result:

    "My name is Harry!"
  • stripTags() : String
    Returns a string with all HTML tags stripped.

    Example:

    "<div id='type'>JavaScript</div>".stripTags();

    Result:

    "JavaScript"
  • trim() : String
    Returns a String with with leading and trailing whitespace removed.

    Example:

    " My name is Harry!   ".trim();

    Result:

    "My name is Harry!"
  • truncate(Number length, String suffix) : String
    Returns a string that is no longer than a certain length.

    Example:

    "JavaScript ".truncate(5);

    Result:

    "Ja..."

    Example:

    "JavaScript ".truncate(5, "#");

    Result:

    "Java#"