Skip to main content
Calculator Library - StringHelper
Praise Magidi avatar
Written by Praise Magidi
Updated over 7 months ago

StringHelper provides a collection of functions for manipulating and working with strings. These functions include trimming, capitalizing, counting occurrences, checking for prefixes and suffixes, padding, and replacing substrings.

This article provides information on the following:

clean(s)

Trim and replace multiple spaces with a single space in a string.

Parameters

  • s (string): The string to clean.

Returns

  • string: The trimmed string.

capitalize(s)

Capitalize the first letter of a string.

Parameters

  • s (string): The string to capitalize.

Returns

  • string: The capitalized string.

Example

let name = "peter"; name = stringHelper.capitalize(name); // name = "Peter"

count(s, subtring)

Returns the number of occurrences of a substring in a string.

Parameters

  • s (string): The string to search.

  • substring (string): The substring to search for.

Returns

  • number: The number of occurrences of the substring in the string.

Example

The count function can be used to count the number of occurrences of a substring in a string. For example, StringHelper.count("hello world", "o") will return 2.

let string = "hello world"; let count = stringHelper.count(string, "o"); // count = 2

startsWith(s, starts, position)

Check whether a string begins with a specified substring at a given position (default: 0).

Parameters

  • s (string): The string to check.

  • starts (string): The substring to check for.

  • position (optional) (number): The position to check from (default: 0).

Returns

  • boolean: true if the string starts with the specified substring, false otherwise.

Example

const string = "Hello, World"; let result = stringHelper.startsWith(string, "Hello"); // result = true

endsWith(s, ends, position)

Check whether a string ends with a specified substring at a given position (default: string.length).

Parameters

  • s (string): The string to check.

  • ends (string): The substring to check for.

  • position (optional) (number): The position to check till (default: string.length).

Returns

  • boolean: true if the string ends with the specified substring, false otherwise.

Example

The endsWith function checks whether a string ends with a specified substring at a given position. For example, StringHelper.endsWith("Hello, world!", "world!") will return true.

padLeft(s, length, pad)

Left-pad a string with a specified character to reach a desired length.

Parameters

  • s (string): The string to pad.

  • length (number): The length to pad to.

  • pad (optional) (string): The character to pad with (default: ' ').

Returns

  • string: The padded string.

Example

let string = "6"; let result = stringHelper.padRight(string, 4, "0"); // result = "6000"

replaceAll(s, search, replacement, ignorecase)

Replace all occurrences of a search string in a string with a specified replacement string. You can also choose to ignore the case when searching.

Parameters

  • s (string): The string to search.

  • search (string): The string to search for.

  • replacement (string): The string to replace the search string with.

  • ignorecase (optional) (boolean): Ignore case when searching (default: false).

Returns

  • string: The string with all occurrences of the search string replaced with the replacement string.

Example

let string = "12345oo789"; string = stringHelper.replaceAll(string,"o",""); // string = 123456789

parseTemplateObject(templateObject, data, options)

Uses a template document and replaces placeholders with actual values. There are 3 different placeholders to use:

  • {{item}} - for strings.

  • {@item} - for strings with URI encoding.

  • @item - for objects and straight templating in. For JSON objects.

Parameters

  • templateObject (string | object) : The template object e.g. {{val1}} , {name: 'Peter', surname: '@surname'}

  • data (object) : data to use when replacing placeholders e.g. {val1: 'Spiderman', surname: 'Parker'}

  • options

Returns

string | object - Returns a string or an object with the placeholders replaced.

Example

// Example 1
let template = {name: 'Peter', surname: '@surname'};
let inputRecord = {val1: 'Spiderman', surname: 'Parker'};
let result = stringHelper.parseTemplateObject(template, inputRecord);
// result = {"name": "Peter","surname": "Parker"}

// Example 2
let template = "{{val1}}";
let inputRecord = {val1: 'Spiderman', surname: 'Parker'};
let result = stringHelper.parseTemplateObject(template, inputRecord);
// result = "Spiderman"

Did this answer your question?