Skip to main content

String methods

Explore the list of string manipulation methods in Actioner with examples.


This documentation provides the list and information about various string manipulation methods available in the stringUtils library.

Info

stringUtils library uses Lodash's String methods. For full details of how to use these methods, refer to Lodash documentation.

Overview

The stringUtils library offers a diverse range of string transformation and manipulation methods. Here's the list of some of the most common methods:

Text transformation

  • camelCase: Converts a string to camel case.
  • capitalize: Converts the first character of a string to uppercase.
  • lowerCase: Converts a string to lowercase.
  • snakeCase: Converts a string to snake case.

Substring manipulation

  • endsWith: Checks if a string ends with a given target string.
  • startsWith: Checks if a string starts with a given target string.
  • trim: Removes leading and trailing whitespace from a string.
  • truncate: Truncates a string to a specified length.

String replacement and formatting

  • escape: Escapes special characters in a string to make it safe for HTML.
  • replace: Replaces occurrences of a pattern in a string with a replacement.
  • template: Compiles a string template with variable placeholders.

Other

  • pad: Pads a string with a specified character to a certain length.
  • padEnd: Pads the end of a string with a specified character to a certain length.
  • padStart: Pads the start of a string with a specified character to a certain length.
  • split: Splits a string into an array of substrings based on a separator.

Methods reference in stringUtils library

stringUtils.camelCase(sourceString)

Converts a string to camel case.

Parameters

  • sourceString (string): The source string to be converted.

Returns

  • (string): The camel cased string.

Example

const sourceString = "hello world";
const camelCased = stringUtils.camelCase(sourceString);
// Result: "helloWorld"

stringUtils.capitalize(sourceString)

Capitalizes the first character of a string.

Parameters

  • sourceString (string): The source string to be capitalized.

Returns

  • (string): The capitalized string.

Example

const sourceString = "hello";
const capitalized = stringUtils.capitalize(sourceString);
// Result: "Hello"

stringUtils.deburr(sourceString)

Removes diacritical marks from a string.

Parameters

  • sourceString (string): The source string to be processed.

Returns

  • (string): The deburred string.

Example

const sourceString = "déjà vu";
const deburred = stringUtils.deburr(sourceString);
// Result: "deja vu"

stringUtils.endsWith(sourceString, target, position)

Checks if a string ends with a given target string.

Parameters

  • sourceString (string): The source string to be checked.
  • target (string): The target string to look for.
  • position (number): Optional. The position within the string to search from (default: sourceString.length).

Returns

  • (boolean): true if the sourceString string ends with the target, otherwise false.

Example

const sourceString = "Hello, world!";
const endsWithWorld = stringUtils.endsWith(sourceString, "world");
// Result: true

stringUtils.escape(sourceString)

Escapes special characters in a string for use in HTML.

Parameters

  • sourceString (string): The source string to be escaped.

Returns

  • (string): The escaped string.

Example

const sourceString = "<script>alert('hello')</script>";
const escaped = stringUtils.escape(sourceString);
// Result: "&lt;script&gt;alert(&#x27;hello&#x27;)&lt;/script&gt;"

stringUtils.escapeRegExp(sourceString)

Escapes special characters in a string for use in a regular expression.

Parameters

  • sourceString (string): The source string to be escaped.

Returns

  • (string): The escaped string.

Example

const sourceString = "[a-z]*";
const escaped = stringUtils.escapeRegExp(sourceString);
// Result: "\\[a\\-z\\]\\*"

stringUtils.kebabCase(sourceString)

Converts a string to kebab case.

Parameters

  • sourceString (string): The source string to be converted.

Returns

  • (string): The kebab cased string.

Example

const sourceString = "Hello World";
const kebabCased = stringUtils.kebabCase(sourceString);
// Result: "hello-world"

stringUtils.lowerCase(sourceString)

Converts a string to lower case.

Parameters

  • sourceString (string): The source string to be converted.

Returns

  • (string): The lower cased string.

Example

const sourceString = "Hello World";
const lowerCased = stringUtils.lowerCase(sourceString);
// Result: "hello world"

stringUtils.lowerFirst(sourceString)

Converts the first character of a string to lower case.

Parameters

  • sourceString (string): The source string to be processed.

Returns

  • (string): The string with the first character in lower case.

Example

const sourceString = "Hello";
const lowerFirst = stringUtils.lowerFirst(sourceString);
// Result: "hello"

stringUtils.pad(sourceString, length, chars)

Pads a string on both sides to a specified length with a given character(s).

Parameters

  • sourceString (string): The source string to be padded.
  • length (number): The target length of the padded string.
  • chars (string): Optional. The character(s) to use for padding (default: " ").

Returns

  • (string): The padded string.

Example

const sourceString = "Hello";
const padded = stringUtils.pad(sourceString, 10, "-");
// Result: "--Hello---"

stringUtils.padEnd(sourceString, length, chars)

Pads a string at the end to a specified length with a given character(s).

Parameters

  • sourceString (string): The source string to be padded.
  • length (number): The target length of the padded string.
  • chars (string): Optional. The character(s) to use for padding (default: " ").

Returns

  • (string): The padded string.

Example

const sourceString = "Hello";
const padded = stringUtils.padEnd(sourceString, 10, "-");
// Result: "Hello-----"

stringUtils.padStart(sourceString, length, chars)

Pads a string at the beginning to a specified length with a given character(s).

Parameters

  • sourceString (string): The source string to be padded.
  • length (number): The target length of the padded string.
  • chars (string): Optional. The character(s) to use for padding (default: " ").

Returns

  • (string): The padded string.

Example

const sourceString = "Hello";
const padded = stringUtils.padStart(sourceString, 10, "-");
// Result: "-----Hello"

stringUtils.parseInt(sourceString, radix)

Converts a string to an integer using a specified radix.

Parameters

  • sourceString (string): The source string to be converted.
  • radix (number): Optional. The base of the numeral system (default: 10).

Returns

  • (number): The parsed integer.

Example

const sourceString = "42";
const parsedInt = stringUtils.parseInt(sourceString);
// Result: 42

stringUtils.repeat(sourceString, n)

Repeats a string a specified number of times.

Parameters

  • sourceString (string): The source string to be repeated.
  • n (number): The number of times to repeat the string.

Returns

  • (string): The repeated string.

Example

const sourceString = "abc";
const repeated = stringUtils.repeat(sourceString, 3);
// Result: "abcabcabc"

stringUtils.replace(sourceString, pattern, replacement)

Replaces occurrences of a pattern within a string.

Parameters

  • sourceString (string): The source string to be processed.
  • pattern (string|RegExp): The pattern to be replaced.
  • replacement (string): The replacement string.

Returns

  • (string): The resulting string after replacements.

Example

const sourceString = "Hello, world!";
const replaced = stringUtils.replace(sourceString, "world", "universe");
// Result: "Hello, universe!"

stringUtils.snakeCase(sourceString)

Converts a string to snake case.

Parameters

  • sourceString (string): The source string to be converted.

Returns

  • (string): The snake cased string.

Example

const sourceString = "Hello World";
const snakeCased = stringUtils.snakeCase(sourceString);
// Result: "hello_world"

stringUtils.split(sourceString, separator, limit)

Splits a string into an array of substrings based on a separator.

Parameters

  • sourceString (string): The source string to be split.
  • separator (string|RegExp): The separator to split the string.
  • limit (number): Optional. The maximum number of splits to perform.

Returns

  • (array): An array of substrings.

Example

const sourceString = "apple,banana,cherry";
const splitArray = stringUtils.split(sourceString, ",");
// Result: ["apple", "banana", "cherry"]

stringUtils.startCase(sourceString)

Converts a string to start case.

Parameters

  • sourceString (string): The source string to be converted.

Returns

  • (string): The start cased string.

Example

const sourceString = "hello world";
const startCased = stringUtils.startCase(sourceString);
// Result: "Hello World"

stringUtils.startsWith(sourceString, target, position)

Checks if a string starts with a given target string.

Parameters

  • sourceString (string): The source string to be checked.
  • target (string): The target string to look for.
  • position (number): Optional. The position within the string to start checking (default: 0).

Returns

  • (boolean): true if the sourceString string starts with the target, otherwise false.

Example

const sourceString = "Hello, world!";
const startsWithHello = stringUtils.startsWith(sourceString, "Hello");
// Result: true

stringUtils.template(sourceString, options)

Creates a compiled template function that can interpolate data.

Parameters

  • sourceString (string): The source string template.
  • options (object): Optional. Options for compiling the template (default: {}).

Returns

  • (function): The compiled template function.

Example

const templateString = "Hello, <%= name %>!";
const compiledTemplate = stringUtils.template(templateString);
const result = compiledTemplate({ name: "Alice" });
// Result: "Hello, Alice!"

stringUtils.toLower(sourceString)

Converts a string to lower case.

Parameters

  • sourceString (string): The source string to be converted.

Returns

  • (string): The lower cased string.

Example

const sourceString = "Hello World";
const lowerCased = stringUtils.toLower(sourceString);
// Result: "hello world"

stringUtils.toUpper(sourceString)

Converts a string to upper case.

Parameters

  • sourceString (string): The source string to be converted.

Returns

  • (string): The upper cased string.

Example

const sourceString = "Hello World";
const upperCased = stringUtils.toUpper(sourceString);
// Result: "HELLO WORLD"

stringUtils.trim(sourceString)

Removes leading and trailing whitespace from a string.

Parameters

  • sourceString (string): The source string to be trimmed.

Returns

  • (string): The trimmed string.

Example

const sourceString = "   Hello, world!   ";
const trimmed = stringUtils.trim(sourceString);
// Result: "Hello, world!"

stringUtils.trimEnd(sourceString)

Removes trailing whitespace from a string.

Parameters

  • sourceString (string): The source string to be trimmed.

Returns

  • (string): The trimmed string.

Example

const sourceString = "   Hello, world!   ";
const trimmedEnd = stringUtils.trimEnd(sourceString);
// Result: " Hello, world!"

stringUtils.trimStart(sourceString)

Removes leading whitespace from a string.

Parameters

  • sourceString (string): The source string to be trimmed.

Returns

  • (string): The trimmed string.

Example

const sourceString = "   Hello, world!   ";
const trimmedStart = stringUtils.trimStart(sourceString);
// Result: "Hello, world! "

stringUtils.truncate(sourceString, options)

Truncates a string to a specified length and appends an ellipsis if necessary.

Parameters

  • sourceString (string): The source string to be truncated.
  • options (object): Optional. Options for truncating the string (default: {}).
  • length (number): The maximum length of the truncated string (default: 30).
  • omission (string): The string to append at the end (default: "...").

Returns

  • (string): The truncated string.

Example

const sourceString = "Lorem ipsum dolor sit amet";
const truncated = stringUtils.truncate(sourceString, { length: 10 });
// Result: "Lorem ipsu..."

stringUtils.unescape(sourceString)

Unescapes HTML entities in a string.

Parameters

  • sourceString (string): The source string to be unescaped.

Returns

  • (string): The unescaped string.

Example

const sourceString = "&lt;script&gt;alert(&#x27;hello&#x27;)&lt;/script&gt;";
const unescaped = stringUtils.unescape(sourceString);
// Result: "<script>alert('hello')</script>"

stringUtils.upperCase(sourceString)

Converts a string to upper case.

Parameters

  • sourceString (string): The source string to be converted.

Returns

  • (string): The upper cased string.

Example

const sourceString = "Hello World";
const upperCased = stringUtils.upperCase(sourceString);
// Result: "HELLO WORLD"

stringUtils.upperFirst(sourceString)

Converts the first character of a string to upper case.

Parameters

  • sourceString (string): The source string to be processed.

Returns

  • (string): The string with the first character in upper case.

Example

const sourceString = "hello";
const upperFirst = stringUtils.upperFirst(sourceString);
// Result: "Hello"

stringUtils.words(sourceString, pattern)

Splits a string into an array of words.

Parameters

  • sourceString (string): The source string to be split.
  • pattern (RegExp|string): Optional. The pattern to match words (default: /\w+/g).

Returns

  • (array): An array of words.

Example

const sourceString = "Hello world!";
const wordsArray = stringUtils.words(sourceString);
// Result: ["Hello", "world"]