Skip to content

Tolki String Utilities List ​

String Utilities List ​

These are the string utilities that can be used independently as single functions.

String Utilities Details ​

after ​

Return the remainder of a string after the first occurrence of a given value.

javascript
import { after } from "@tolki/str";

const result = after("This is my name", "This is");

// result is " my name"

▶ Try it

Result" my name"

afterLast ​

Return the remainder of a string after the last occurrence of a given value.

javascript
import { afterLast } from "@tolki/str";

const result = afterLast("App\Http\Controllers\Controller", "\\");

// result is "Controller"

▶ Try it

Result"Controller"

apa ​

Convert the given string to APA-style title case.

javascript
import { apa } from "@tolki/str";

const result = apa("Creating A Project");

// result is "Creating a Project"

▶ Try it

Result"Creating a Project"

ascii ​

Transliterate a UTF-8 value to ASCII.

Uses the transliteration package.

javascript
import { ascii } from "@tolki/str";

const result = ascii("û");

// result is "u"

▶ Try it

Result"u"

before ​

Get the portion of a string before the first occurrence of a given value.

javascript
import { before } from "@tolki/str";

const result = before("This is my name", "my");

// result is "This is "

▶ Try it

Result"This is "

beforeLast ​

Get the portion of a string before the last occurrence of a given value.

javascript
import { beforeLast } from "@tolki/str";

const result = beforeLast("This is my name", "is");

// result is "This "

▶ Try it

Result"This "

between ​

Get the portion of a string between two given values.

javascript
import { between } from "@tolki/str";

const result = between("This is my name", "This", "name");

// result is " is my "

▶ Try it

Result" is my "

betweenFirst ​

Get the smallest possible portion of a string between two given values.

javascript
import { betweenFirst } from "@tolki/str";

const result = betweenFirst("[a] bc [d]", "[", "]");

// result is "a"

▶ Try it

Result"a"

camel ​

Convert a value to camel case.

javascript
import { camel } from "@tolki/str";

const result = camel("foo_bar");

// result is "fooBar"

▶ Try it

Result"fooBar"

charAt ​

Get the character at the specified index.

javascript
import { charAt } from "@tolki/str";

const result = charAt("This is my name.", 6);

// result is "s"

▶ Try it

Result"s"

chopStart ​

Remove the given string(s) if it exists at the start of the haystack.

javascript
import { chopStart } from "@tolki/str";

const result = chopStart("https://laravel.com", "https://");

// result is "laravel.com"

▶ Try it

Result"laravel.com"

You may also pass an array of string as the second argument:

javascript
import { chopStart } from "@tolki/str";

const result = chopStart("http://laravel.com", ["https://", "http://"]);

// result is "laravel.com"

▶ Try it

Result"laravel.com"

chopEnd ​

Remove the given string(s) if it exists at the end of the haystack.

javascript
import { chopEnd } from "@tolki/str";

const result = chopEnd("app/Models/Photograph.php", ".php");

// result is "app/Models/Photograph"

▶ Try it

Result"app/Models/Photograph"

You may also pass an array of string as the second argument:

javascript
import { chopEnd } from "@tolki/str";

const result = chopEnd("laravel.com/index.php", ["/index.html", "/index.php"]);

// result is "laravel.com"

▶ Try it

Result"laravel.com"

contains ​

Determine if a given string contains a given substring.

javascript
import { contains } from "@tolki/str";

const result = contains("This is my name", "my");

// result is true

You may also pass an array of strings as the second argument:

javascript
import { contains } from "@tolki/str";

const result = contains("This is my name", ["my", "foo"]);

// result is true

You may also disable case sensitivity by passing true as the third argument:

javascript
import { contains } from "@tolki/str";

const result = contains("This is my name", "MY", true);

// result is true

▶ Try it

Resulttrue

containsAll ​

Determine if a given string contains all array values.

javascript
import { containsAll } from "@tolki/str";

const result = containsAll("This is my name", ["my", "name"]);

// result is true

You may also disable case sensitivity by passing true as the second argument:

javascript
import { containsAll } from "@tolki/str";

const result = containsAll("This is my name", ["MY", "NAME"], true);

// result is true

▶ Try it

Resulttrue

counted ​

Get the plural form of an English word with the count prepended.

javascript
import { counted } from "@tolki/str";

const result = counted("order", 1);

// result is "1 order"

const result2 = counted("order", 1000);

// result2 is "1,000 orders"

You may also pass an array as the count, in which case its length is used:

javascript
import { counted } from "@tolki/str";

const result = counted("order", ["a", "b"]);

// result is "2 orders"

▶ Try it

Result"1,000 orders"

doesntContain ​

Determine if a given string doesn't contain a given substring.

javascript
import { doesntContain } from "@tolki/str";

const result = doesntContain("This is name", "my");

// result is true

You may also pass an array of strings as the second argument:

javascript
import { doesntContain } from "@tolki/str";

const result = doesntContain("This is name", ["my", "framework"]);

// result is true

You may also disable case sensitivity by passing true as the third argument:

javascript
import { doesntContain } from "@tolki/str";

const result = doesntContain("This is name", "MY", true);

// result is true

▶ Try it

Resulttrue

deduplicate ​

Replace consecutive instances of a given character with a single character in the given string.

javascript
import { deduplicate } from "@tolki/str";

const result = deduplicate("The   Laravel   Framework");

// result is "The Laravel Framework"

You can specify which character to deduplicate as the second argument (defaults to space):

javascript
import { deduplicate } from "@tolki/str";

const result = deduplicate("The---Laravel---Framework", "-");

// result is "The-Laravel-Framework"

You can also specify multiple characters to deduplicate by passing an array as the second argument:

javascript
import { deduplicate } from "@tolki/str";

const result = deduplicate("The---Laravel   Framework", ["-", " "]);

// result is "The-Laravel Framework"

▶ Try it

Result"The-Laravel Framework"

doesntEndWith ​

Determine if a given string doesn't end with a given substring.

javascript
import { doesntEndWith } from "@tolki/str";

const result = doesntEndWith("This is my name", "dog");

// result is true

You may also pass an array of strings as the second argument. If the string ends with any of the substrings, the function will return false.

javascript
import { doesntEndWith } from "@tolki/str";

const result = doesntEndWith("This is my name", ["this", "foo"]);

// result is true

const result2 = doesntEndWith("This is my name", ["name", "foo"]);

// result2 is false

▶ Try it

Resulttrue

doesntStartWith ​

Determine if a given string doesn't start with a given substring.

javascript
import { doesntStartWith } from "@tolki/str";

const result = doesntStartWith("This is my name", "That");

// result is true

You may also pass an array of strings as the second argument. If the string starts with any of the substrings, the function will return false.

javascript
import { doesntStartWith } from "@tolki/str";

const result = doesntStartWith("This is my name", ["this", "foo"]);

// result is true

const result2 = doesntStartWith("This is my name", ["name", "foo"]);

// result2 is true

▶ Try it

Resulttrue

endsWith ​

Determine if a given string ends with a given substring.

javascript
import { endsWith } from "@tolki/str";

const result = endsWith("This is my name", "name");

// result is true

You may also pass an array of strings as the second argument to determine if the string ends with any of the substrings.

javascript
import { endsWith } from "@tolki/str";

const result = endsWith("This is my name", ["name", "foo"]);

// result is true

const result2 = endsWith("This is my name", ["this", "foo"]);

// result2 is false

▶ Try it

Resulttrue

excerpt ​

Extracts an excerpt from text that matches the first instance of a phrase.

javascript
import { excerpt } from "@tolki/str";

const result = excerpt("This is my name", "my", { radius: 3 });

// result is "...is my na..."

The radius option, which defaults to 100, allows you to define the number of characters that should appear on each side of the truncated string.

In addition, you may use the omission option to define the string that will be prepended and appended to the truncated string:

javascript
import { excerpt } from "@tolki/str";

const result = excerpt("This is my name", "name", {
  radius: 3,
  omission: "(...) ",
});

// result is "(...) my name"

▶ Try it

Result"...is my na..."

finish ​

Cap a string with a single instance of a given value.

javascript
import { finish } from "@tolki/str";

const result = finish("this/string", "/");

// result is 'this/string/'

const result2 = finish("this/string/", "/");

// result2 is also 'this/string/'

▶ Try it

Result"this/string/"

fromBase64 ​

Decode the given Base64 encoded string.

javascript
import { fromBase64 } from "@tolki/str";

const result = fromBase64("TGFyYXZlbA==");

// result is 'Laravel'

▶ Try it

Result"Laravel"

headline ​

Convert the given string to proper case for each word.

javascript
import { headline } from "@tolki/str";

const result = headline("steve_jobs");

// result is "Steve Jobs"

const result2 = headline("EmailNotificationSent");

// result2 is "Email Notification Sent"

▶ Try it

Result"Email Notification Sent"

initials ​

Get the "initials" representing each word in the provided string, optionally capitalizing.

javascript
import { initials } from "@tolki/str";

const result = initials("taylor otwell");

// result is "to"

const result2 = initials("taylor otwell", true);

// result2 is "TO"

▶ Try it

Result"to"

inlineMarkdown ​

Converts inline Markdown into HTML.

Uses the markdown-it package.

javascript
import { inlineMarkdown } from "@tolki/str";

const result = inlineMarkdown("This is **bold** and this is *italic*.");

// result is 'This is <strong>bold</strong> and this is <em>italic</em>.'

Inline Markdown Security ​

By default, the inlineMarkdown function disables raw HTML and unsafe links (e.g., javascript: URLs) to prevent XSS attacks. You can enable raw HTML by passing { html: true } in the options.

javascript
import { inlineMarkdown } from "@tolki/str";

const result = inlineMarkdown(
  "This is <strong>bold</strong> and this is <em>italic</em>.",
  { html: true },
);

// result is 'This is <strong>bold</strong> and this is <em>italic</em>.'

You can also enable unsafe links by passing { allowUnsafeLinks: true } in the options.

javascript
import { inlineMarkdown } from "@tolki/str";

const result = inlineMarkdown("[click me](javascript:alert(1))", {
  allowUnsafeLinks: true,
});

// result is '<a href="javascript:alert(1)">click me</a>'

▶ Try it

Result"[click me](javascript:alert(1))"

is ​

Determine if a given string matches a given pattern.

javascript
import { is } from "@tolki/str";

const result = is("foo*", "foobar");

// result is true

const result2 = is("baz*", "foobar");

// result2 is false

You can disable case sensitivity by passing true as the third argument:

javascript
import { is } from "@tolki/str";

const result = is("*.jpg", "photo.JPG", true);

// result is true

▶ Try it

Resulttrue

isAscii ​

Determine if a given string is 7-bit ASCII.

javascript
import { isAscii } from "@tolki/str";

const result = isAscii("Taylor");

// result is true

const result2 = isAscii("û");

// result2 is false

▶ Try it

Resulttrue

isJson ​

Determine if a given value is valid JSON.

javascript
import { isJson } from "@tolki/str";

const result = isJson("[1,2,3]");

// result is true

const result2 = isJson('{"first": "John", "last": "Doe"}');

// result2 is true

const result3 = isJson('{first: "John", last: "Doe"}');

// result3 is false

▶ Try it

Resulttrue

isUrl ​

Determine if a given value is a valid URL.

javascript
import { isUrl } from "@tolki/str";

const result = isUrl("https://example.com");

// result is true

const result2 = isUrl("tolki js");

// result2 is false

You may also specify an array of allowed protocols as the second argument:

javascript
import { isUrl } from "@tolki/str";

const result = isUrl("https://example.com", ["https", "http"]);

// result is true

const result2 = isUrl("http://example.com", ["https"]);

// result2 is false

▶ Try it

Resulttrue

isUlid ​

Determine if a given value is a valid ULID.

javascript
import { isUlid } from "@tolki/str";

const result = isUlid("01gd6r360bp37zj17nxb55yv40");

// result is true

const result2 = isUlid("tolkijs");

// result2 is false

▶ Try it

Resulttrue

isUuid ​

Determine if a given value is a valid UUID.

Uses the uuid package.

javascript
import { isUuid } from "@tolki/str";

const result = isUuid("a0a2a2d2-0b87-4a18-83f2-2529882be2de");

// result is true

const result2 = isUuid("tolkijs");

// result2 is false

You may also validate that the given UUID matches a UUID specification by version (1, 3, 4, 5, 6, 7, or 8):

javascript
import { isUuid } from "@tolki/str";

const result = isUuid("a0a2a2d2-0b87-4a18-83f2-2529882be2de", 4);

// result is true

const result2 = isUuid("a0a2a2d2-0b87-4a18-83f2-2529882be2de", 5);

// result2 is false

▶ Try it

Resulttrue

kebab ​

Convert a string to kebab case.

javascript
import { kebab } from "@tolki/str";

const result = kebab("fooBar");

// result is "foo-bar"

▶ Try it

Result"foo-bar"

lcfirst ​

Make a string's first character lowercase.

javascript
import { lcfirst } from "@tolki/str";

const result = lcfirst("Foo Bar");

// result is "foo Bar"

▶ Try it

Result"foo Bar"

length ​

Return the length of the given string.

javascript
import { length } from "@tolki/str";

const result = length("Tolki JS");

// result is 8

▶ Try it

Result8

limit ​

Limit the number of characters in a string.

javascript
import { limit } from "@tolki/str";

const result = limit("The quick brown fox jumps over the lazy dog", 20);

// result is "The quick brown fox..."

You may pass a third argument to specify the string to append to the truncated string (defaults to an ellipsis):

javascript
import { limit } from "@tolki/str";

const result = limit(
  "The quick brown fox jumps over the lazy dog",
  20,
  " (...)",
);

// result is "The quick brown fox (...)"

You may also pass a fourth argument to indicate whether to avoid cutting off words (defaults to false):

javascript
import { limit } from "@tolki/str";

const result = limit(
  "The quick brown fox jumps over the lazy dog",
  12,
  "...",
  true,
);

// result is "The quick..."

▶ Try it

Result"The quick brown fox..."

lower ​

Convert the given string to lower-case.

javascript
import { lower } from "@tolki/str";

const result = lower("LARAVEL");

// result is "laravel"

▶ Try it

Result"laravel"

markdown ​

Converts GitHub flavored Markdown into HTML.

Uses the markdown-it package.

javascript
import { markdown } from "@tolki/str";

const result = markdown("# Laravel");

// result is "<h1>Laravel</h1>"

Markdown Security ​

By default, the markdown function disables raw HTML and unsafe links (e.g., javascript: URLs) to prevent XSS attacks. You can enable raw HTML by passing { html: true } in the options.

javascript
import { markdown } from "@tolki/str";

const result = markdown(
  "This is <strong>bold</strong> and this is <em>italic</em>.",
  { html: true },
);

// result is '<p>This is <strong>bold</strong> and this is <em>italic</em>.</p>'

You can also enable unsafe links by passing { allowUnsafeLinks: true } in the options.

javascript
import { markdown } from "@tolki/str";

const result = markdown("[click me](javascript:alert(1))", {
  allowUnsafeLinks: true,
});

// result is '<p><a href="javascript:alert(1)">click me</a></p>'

▶ Try it

Result"<p>[click me](javascript:alert(1))</p>\n"

mask ​

Masks a portion of a string with a repeated character.

javascript
import { mask } from "@tolki/str";

const result = mask("taylor@example.com", "*", 3);

// result is "tay***************"

If needed, you may provide a negative value for the third argument, which instructs the function to begin masking from the end of the string. A fourth argument may also be provided to specify the number of masked characters.

javascript
import { mask } from "@tolki/str";

const result = mask("taylor@example.com", "*", -15, 3);

// result is "tay***@example.com"

▶ Try it

Result"tay***@example.com"

match ​

Get the string matching the given pattern.

javascript
import { match } from "@tolki/str";

const result = match("/bar/", "foo bar");

// result is "bar"

const result2 = match("/foo (.*)/", "foo bar");

// result2 is "bar"

▶ Try it

Result"bar"

matchAll ​

Get the string(s) matching the given pattern.

javascript
import { matchAll } from "@tolki/str";

const result = matchAll("/bar/", "bar foo bar");

// result is ["bar", "bar"]

If no matches are found, an empty array will be returned.

▶ Try it

Result["bar","bar"]

isMatch ​

Determine if a given string matches a given pattern.

javascript
import { isMatch } from "@tolki/str";

const result = isMatch("/foo (.*)/", "foo bar");

// result is true

const result2 = isMatch("/foo (.*)/", "laravel");

// result2 is false

▶ Try it

Resulttrue

orderedUuid ​

This function is purposely not implemented. Use the uuid7() function instead to generate a UUIDv7, which is a time-ordered UUID.

See more details on this StackOverflow discussion.

numbers ​

Remove all non-numeric characters from a string.

javascript
import { numbers } from "@tolki/str";

const result = numbers("(555) 123-4567");

// result is "5551234567"

const result2 = numbers("L4r4v3l!");

// result2 is "443"

const result3 = numbers(["(555) 123-4567", "L4r4v3l!"]);

// result3 is ["5551234567", "443"]

▶ Try it

Result"5551234567"

padBoth ​

Pad both sides of a string with another string to a certain length.

javascript
import { padBoth } from "@tolki/str";

const result = padBoth("James", 10, "_");

// result is "__James__"

const result2 = padBoth("James", 10);

// result2 is "  James   "

▶ Try it

Result"__James___"

padLeft ​

Pad the left side of a string with another string to a certain length.

javascript
import { padLeft } from "@tolki/str";

const result = padLeft("James", 10, "-=");

// result is "-=-=-James"

const result2 = padLeft("James", 10);

// result2 is "     James"

▶ Try it

Result"-=-=-James"

padRight ​

Pad the right side of a string with another string to a certain length.

javascript
import { padRight } from "@tolki/str";

const result = padRight("James", 10, "-");

// result is "James-----"

const result2 = padRight("James", 10);

// result2 is "James     "

▶ Try it

Result"James-----"

pascal ​

Convert a value to Pascal case.

javascript
import { pascal } from "@tolki/str";

const result = pascal("hello world");

// result is "HelloWorld"

▶ Try it

Result"HelloWorld"

pluralPascal ​

Pluralize the last word of an English, Pascal caps case string.

javascript
import { pluralPascal } from "@tolki/str";

const result = pluralPascal("HelloWorld");

// result is "HelloWorlds"

▶ Try it

Result"HelloWorlds"

password ​

INFO

password, random, ulid, uuid, and uuid7 (along with their createXUsing/createXNormally testing helpers) generate a new value on every call, so a live playground would just show a different, unverifiable result each time you touch it — there's no live example for these.

Generate a random, secure password. It is 32 characters long by default and mixes letters, numbers and symbols; the other arguments turn letters, numbers, symbols and spaces on or off.

javascript
import { password } from "@tolki/str";

const result = password();

// result is a random, secure password

const pin = password(6, false, true, false, false);

// pin is six random digits

The password includes at least one character from each set you turn on, if it is long enough to hold one of each. Turning every set off throws an Error.

plural ​

Get the plural form of an English word.

javascript
import { plural } from "@tolki/str";

const result = plural("car");

// result is "cars"

const result2 = plural("child");

// result2 is "children"

You may provide a second argument to specify the count. If the count is 1, the singular form will be returned.

javascript
import { plural } from "@tolki/str";

const result = plural("child", 2);

// result is "children"

const result2 = plural("child", 1);

// result2 is "child"

You pass a third argument to prepend the count to the resulting string.

javascript
import { plural } from "@tolki/str";

const result = plural("car", 1000, true);

// result is "1,000 cars"

▶ Try it

Result"1,000 cars"

pluralStudly ​

Pluralize the last word of an English, studly caps case string.

javascript
import { pluralStudly } from "@tolki/str";

const result = pluralStudly("VerifiedHuman");

// result is "VerifiedHumans"

const result2 = pluralStudly("UserFeedback");

// result2 is "UserFeedback"

▶ Try it

Result"VerifiedHumans"

position ​

Find the multi-byte safe position of the first occurrence of a given substring in a string.

javascript
import { position } from "@tolki/str";

const result = position("Hello, World!", "Hello");

// result is 0

const result2 = position("Hello, World!", "W");

// result2 is 7

▶ Try it

Result7

random ​

Generate a more truly "random" alpha-numeric string.

javascript
import { random } from "@tolki/str";

const result = random(40);

// result is a random 40-character alpha-numeric string

During testing, you can use the createRandomStringsUsing function to provide a custom random string generator for predictable results.

javascript
import { createRandomStringsUsing, random } from "@tolki/str";

createRandomStringsUsing((length) => "A".repeat(length));

const result = random(5);

// result is "AAAAA"

You can reset to the default random string generator by calling the createRandomStringsNormally function.

javascript
import {
  createRandomStringsUsing,
  createRandomStringsNormally,
  random,
} from "@tolki/str";

createRandomStringsUsing((length) => "A".repeat(length));

const result = random(5);

// result is "AAAAA"

createRandomStringsNormally();

const result2 = random(5);

// result is a random 5-character alpha-numeric string

remove ​

Remove any occurrence of the given string in the subject.

javascript
import { remove } from "@tolki/str";

const result = remove("e", "Peter Piper picked a peck of pickled peppers.");

// result is "Ptr Pipr pickd a pck of pickld ppprs."

You can pass false as the third argument to disable case sensitivity:

javascript
import { remove } from "@tolki/str";

const result = remove(
  "e",
  "Peter Piper picked a peck of pickled peppers.",
  false,
);

// result is "Ptr Pipr pickd a pck of pickld ppprs."

The subject can also be a list, an object or a Map of strings, and the result keeps its shape and keys. A collection gives back what its all() holds, and any other iterable gives a list:

javascript
import { remove } from "@tolki/str";

const result = remove("o", { first: "Foo", last: "Boo" });

// result is { first: "F", last: "B" }

▶ Try it

Result"Ptr Pipr pickd a pck of pickld ppprs."

repeat ​

Repeat the given string.

javascript
import { repeat } from "@tolki/str";

const result = repeat("a", 5);

// result is "aaaaa"

▶ Try it

Result"aaaaa"

replace ​

Replace the given value in the given string.

javascript
import { replace } from "@tolki/str";

const result = replace("11.x", "12.x", "Laravel 11.x");

// result is "Laravel 12.x"

The replace function also accepts a fourth argument to disable case sensitivity:

javascript
import { replace } from "@tolki/str";

const result = replace(
  "php",
  "Laravel",
  "PHP Framework for Web Artisans",
  false,
);

// result is "Laravel Framework for Web Artisans"

The subject can also be a list, an object or a Map of strings, and the result keeps its shape and keys. A collection gives back what its all() holds, and any other iterable gives a list:

javascript
import { replace } from "@tolki/str";

const result = replace("o", "0", { first: "Foo", last: "Boo" });

// result is { first: "F00", last: "B00" }

▶ Try it

Result"Laravel Framework for Web Artisans"

replaceArray ​

Replace a given value in the string sequentially with an array.

javascript
import { replaceArray } from "@tolki/str";

const result = replaceArray(
  "?",
  ["8:30", "9:00"],
  "The event will take place between ? and ?",
);

// result is "The event will take place between 8:30 and 9:00"

▶ Try it

Result"The event will take place between 8:30 and 9:00"

replaceFirst ​

Replace the first occurrence of a given value in the string.

javascript
import { replaceFirst } from "@tolki/str";

const result = replaceFirst(
  "the",
  "a",
  "the quick brown fox jumps over the lazy dog",
);

// result is "a quick brown fox jumps over the lazy dog"

▶ Try it

Result"a quick brown fox jumps over the lazy dog"

replaceLast ​

Replace the last occurrence of a given value in the string.

javascript
import { replaceLast } from "@tolki/str";

const result = replaceLast(
  "the",
  "a",
  "the quick brown fox jumps over the lazy dog",
);

// result is "the quick brown fox jumps over a lazy dog"

▶ Try it

Result"the quick brown fox jumps over a lazy dog"

replaceMatches ​

Replace the patterns matching the given regular expression.

javascript
import { replaceMatches } from "@tolki/str";

const result = replaceMatches(/[^A-Za-z0-9]+/g, "", "(+1) 501-555-1000");

// result is "15015551000"

▶ Try it

Result"15015551000"

The replaceMatches function also accepts a closure as the second argument, allowing you to perform the replacement logic within the closure and return the replaced value.

javascript
import { replaceMatches } from "@tolki/str";

const result = replaceMatches(
  /\d+/g,
  (matches) => `number ${matches[0]}`,
  "My numbers are 123 and 456.",
);

// result is "My numbers are number 123 and number 456."

▶ Try it

Result"My numbers are number 123 and number 456."

replaceStart ​

Replace the first occurrence of the given value if it appears at the start of the string.

javascript
import { replaceStart } from "@tolki/str";

const result = replaceStart("Hello", "Laravel", "Hello World");

// result is "Laravel World"

const result2 = replaceStart("World", "Laravel", "Hello World");

// result2 is "Hello World"

▶ Try it

Result"Laravel World"

replaceEnd ​

Replace the last occurrence of a given value if it appears at the end of the string.

javascript
import { replaceEnd } from "@tolki/str";

const result = replaceEnd("World", "Laravel", "Hello World");

// result is "Hello Laravel"

const result2 = replaceEnd("Hello", "Laravel", "Hello World");

// result2 is "Hello World"

▶ Try it

Result"Hello Laravel"

reverse ​

Reverse the given string.

javascript
import { reverse } from "@tolki/str";

const result = reverse("Hello World");

// result is "dlroW olleH"

▶ Try it

Result"dlroW olleH"

singular ​

Get the singular form of an English word.

Uses the pluralize package.

javascript
import { singular } from "@tolki/str";

const result = singular("cars");

// result is "car"

const result2 = singular("children");

// result2 is "child"

▶ Try it

Result"child"

slug ​

Generate a URL-friendly "slug" from a given string.

javascript
import { slug } from "@tolki/str";

const result = slug("Laravel 5 Framework", "-");

// result is "laravel-5-framework"

▶ Try it

Result"laravel-5-framework"

snake ​

Convert a string to snake case.

javascript
import { snake } from "@tolki/str";

const result = snake("fooBar");

// result is "foo_bar"

const result2 = snake("fooBar", "-");

// result2 is "foo-bar"

▶ Try it

Result"foo_bar"

squish ​

Remove all "extra" blank space from the given string.

javascript
import { squish } from "@tolki/str";

const result = squish("    laravel    framework    ");

// result is "laravel framework"

▶ Try it

Result"laravel framework"

start ​

Begin a string with a single instance of a given value.

javascript
import { start } from "@tolki/str";

const result = start("this/string", "/");

// result is "/this/string"

const result2 = start("/this/string", "/");

// result2 is also '/this/string'

▶ Try it

Result"/this/string"

startsWith ​

Determine if a given string starts with a given substring.

javascript
import { startsWith } from "@tolki/str";

const result = startsWith("This is my name", "This");

// result is true

The second argument may also be an array of strings to check against. If the string starts with any of the substrings, the function will return true.

javascript
import { startsWith } from "@tolki/str";

const result = startsWith("This is my name", ["This", "That", "There"]);

// result is true

▶ Try it

Resulttrue

stripTags ​

Strip HTML tags from a string.

javascript
import { stripTags } from "@tolki/str";

const result = stripTags("<p>Hello <strong>World</strong></p>");

// result is "Hello World"

▶ Try it

Result"Hello World"

studly ​

Convert a value to studly caps case.

javascript
import { studly } from "@tolki/str";

const result = studly("foo_bar");

// result is "FooBar"

▶ Try it

Result"FooBar"

substr ​

Returns the portion of the string specified by the start and length parameters.

javascript
import { substr } from "@tolki/str";

const result = substr("The Laravel Framework", 4, 7);

// result is "Laravel"

▶ Try it

Result"Laravel"

substrCount ​

Returns the number of substring occurrences.

javascript
import { substrCount } from "@tolki/str";

const result = substrCount(
  "If you like ice cream, you will like snow cones.",
  "like",
);

// result is 2

Offsets and lengths count characters, so an emoji counts as one. An empty needle throws, as it does in PHP.

▶ Try it

Result2

substrReplace ​

Replace text within a portion of a string.

javascript
import { substrReplace } from "@tolki/str";

const result = substrReplace("1300", ":", 2);

// result is "13:"

const result2 = substrReplace("1300", ":", 2, 0);

// result2 is "13:00"

You may also pass an array or plain object of strings as the first argument to replace text in each of them at once, keeping the keys intact:

javascript
const results = substrReplace(["INV-1234", "INV-5678"], "****", 4, 4);

// results is ["INV-****", "INV-****"]

The replace, offset, and length arguments may each be given as an array to apply a different value to each string by position. When the first argument is a single string, an array of replacements uses only the first replacement, and an array offset or length throws a TypeError.

▶ Try it

Result"13:00"

swap ​

Swap multiple keywords in a string with other keywords.

javascript
import { swap } from "@tolki/str";

const result = swap(
  {
    Tacos: "Burritos",
    great: "fantastic",
  },
  "Tacos are great!",
);

// result is "Burritos are fantastic!"

▶ Try it

Result"Burritos are fantastic!"

take ​

Take the first or last {$limit} characters of a string.

javascript
import { take } from "@tolki/str";

const result = take("Build something amazing!", 5);

// result is "Build"

const result2 = take("Build something amazing!", -5);

// result2 is "zing!"

▶ Try it

Result"Build"

title ​

Convert the given string to proper case.

javascript
import { title } from "@tolki/str";

const result = title("a nice title uses the correct case");

// result is "A Nice Title Uses The Correct Case"

▶ Try it

Result"A Nice Title Uses The Correct Case"

toBase64 ​

Convert the given string to Base64 encoding.

javascript
import { toBase64 } from "@tolki/str";

const result = toBase64("Laravel");

// result is "TGFyYXZlbA=="

▶ Try it

Result"TGFyYXZlbA=="

trans ​

This function is purposely not implemented. Laravel's Str::trans resolves translation keys through the framework's localization system, which has no equivalent in this library. Use a JavaScript i18n library (such as i18next or FormatJS) to translate strings instead.

transliterate ​

Transliterate a string to its closest ASCII representation.

Uses the any-ascii package.

javascript
import { transliterate } from "@tolki/str";

const result = transliterate("Æneid");

// result is "Aeneid"

const result2 = transliterate("ⓣⓔⓢⓣ@ⓛⓐⓡⓐⓥⓔⓛ.ⓒⓞⓜ");

// result2 is "test@laravel.com"

▶ Try it

Result"Aeneid"

trim ​

Remove all whitespace from both ends of a string.

javascript
import { trim } from "@tolki/str";

const result = trim(" foo bar ");

// result is "foo bar"

To trim other characters instead of whitespace, pass them as a second argument. ltrim and rtrim take it too. An empty string trims nothing, not even whitespace:

javascript
import { trim } from "@tolki/str";

const result = trim("-foo  bar_", "-_");

// result is "foo  bar"

const result2 = trim(" foo bar ", "");

// result2 is " foo bar "

▶ Try it

Result"foo bar"

ltrim ​

Remove all whitespace from the beginning of a string.

javascript
import { ltrim } from "@tolki/str";

const result = ltrim("  foo bar  ");

// result is "foo bar  "

▶ Try it

Result"foo bar "

rtrim ​

Remove all whitespace from the end of a string.

javascript
import { rtrim } from "@tolki/str";

const result = rtrim("  foo bar  ");

// result is "  foo bar"

▶ Try it

Result" foo bar"

ucfirst ​

Make a string's first character uppercase.

javascript
import { ucfirst } from "@tolki/str";

const result = ucfirst("foo bar");

// result is "Foo bar"

▶ Try it

Result"Foo bar"

ucsplit ​

Split a string into pieces by uppercase characters.

javascript
import { ucsplit } from "@tolki/str";

const result = ucsplit("FooBar");

// result is ["Foo", "Bar"]

▶ Try it

Result["Foo","Bar"]

ucwords ​

Uppercase the first letter of each word in a string.

javascript
import { ucwords } from "@tolki/str";

const result = ucwords("laravel framework");

// result is "Laravel Framework"

▶ Try it

Result"Laravel Framework"

upper ​

Convert the given string to upper-case.

javascript
import { upper } from "@tolki/str";

const result = upper("laravel");

// result is "LARAVEL"

▶ Try it

Result"LARAVEL"

ulid ​

Generate a ULID (Universally Unique Lexicographically Sortable Identifier).

Uses the ulid package.

javascript
import { ulid } from "@tolki/str";

const result = ulid();

// result is "01F8MECHZX2D7J8F8C8D4B8F8C"

During testing, you can use the createUlidsUsing function to provide a custom ULID generator for predictable results.

javascript
import { createUlidsUsing, ulid } from "@tolki/str";

createUlidsUsing(() => "custom-ulid");

const result = ulid();

// result is "custom-ulid"

You can reset to the default ULID generator by calling the createUlidsNormally function.

javascript
import { createUlidsUsing, createUlidsNormally, ulid } from "@tolki/str";

createUlidsUsing(() => "custom-ulid");

const result = ulid();

// result is "custom-ulid"

createUlidsNormally();

const result2 = ulid();

// result is a randomly generated ULID

unwrap ​

Unwrap the string with the given strings.

javascript
import { unwrap } from "@tolki/str";

const result = unwrap("-Laravel-", "-");

// result is "Laravel"

const result2 = unwrap('{framework: "Laravel"}', "{", "}");

// result2 is 'framework: "Laravel"'

▶ Try it

Result"framework: Laravel"

uuid ​

Generate a UUID (version 4).

Uses the uuid package.

javascript
import { uuid } from "@tolki/str";

const result = uuid();

// result is a randomly generated UUID (version 4)

During testing, you can use the createUuidsUsing function to provide a custom UUID generator for predictable results.

javascript
import { createUuidsUsing, uuid } from "@tolki/str";

createUuidsUsing(() => "custom-uuid");

const result = uuid();

// result is "custom-uuid"

You can reset to the default UUID generator by calling the createUuidsNormally function.

javascript
import { createUuidsUsing, createUuidsNormally, uuid } from "@tolki/str";

createUuidsUsing(() => "custom-uuid");

const result = uuid();

// result is "custom-uuid"

createUuidsNormally();

const result2 = uuid();

// result is a randomly generated UUID

uuid7 ​

Generate a UUID (version 7).

Uses the uuid package.

javascript
import { uuid7 } from "@tolki/str";

const result = uuid7();

// result is a randomly generated UUID (version 7)

The uuid7() function also uses the createUuidsUsing and createUuidsNormally functions for testing purposes, as described in the uuid function above.

wordCount ​

Get the number of words a string contains.

javascript
import { wordCount } from "@tolki/str";

const result = wordCount("Hello, world!");

// result is 2

▶ Try it

Result2

wordWrap ​

Wrap a string to a given number of characters.

The width is counted in characters rather than in UTF-16 code units, so characters outside the Basic Multilingual Plane such as emoji each count as a single character.

javascript
import { wordWrap } from "@tolki/str";

const result = wordWrap(
  "The quick brown fox jumped over the lazy dog.",
  20,
  "<br />\n",
);

// result is:
/*
The quick brown fox<br />
jumped over the lazy<br />
dog.
*/

const result2 = wordWrap("😀😀😀😀", 2, "\n", true);

// result2 is "😀😀\n😀😀"

▶ Try it

Result"The quick brown fox<br />jumped over the lazy<br />dog."

words ​

Limit the number of words in a string.

javascript
import { words } from "@tolki/str";

const result = words("Perfectly balanced, as all things should be.", 3, " >>>");

// result is "Perfectly balanced, as >>>"

▶ Try it

Result"Perfectly balanced, as >>>"

wrap ​

Wrap the string with the given strings.

javascript
import { wrap } from "@tolki/str";

const result = wrap("Laravel", '"');

// result is '"Laravel"'

const result2 = wrap("is", "This ", " Laravel!");

// result2 is 'This is Laravel!'

▶ Try it

Result"This is Laravel!"

str ​

WARNING

Using the str or of functions is discouraged for frontend projects because it will import the entire Stringable class, all of its methods, and all 3rd party dependencies into your final bundle, which may significantly increase its size.

For frontend projects, it is recommended to use the individual string functions instead for better tree-shaking and smaller bundle sizes. In Rolldown and Rspack, even an unused top-level str(...) or of(...) call can leave the class behind unless the call is written /* @__PURE__ */; see Bundling and Tree-Shaking.

Also unlike the standalone functions on this page, str/of return a Stringable class instance rather than a plain value, so there's no live playground for them here — try the fluent methods it exposes (like .append() or .snake()) directly in your own code instead.

Get a new Stringable object from the given string.

javascript
import { str } from "@tolki/str";

const result = str("Laravel").append(" Otwell");

// result is a Stringable class instance representing "Laravel Otwell"

If no string is provided, a Stringable class instance with an empty string will be returned.

javascript
import { str } from "@tolki/str";

const result = str().append("FooBar").snake();

// result is a Stringable class instance representing "foo_bar"

str() always returns a Stringable, so pass it the string you want to work on, as in str("FooBar").snake(), or call the standalone function, snake("FooBar"). Laravel's str() with no argument forwards a call such as str()->snake('FooBar') to Str::snake(), but this package's str() does not.

of ​

The of function is an alias for the str function made for parity with Laravels' Str::of method. See the str function documentation for details.

javascript
import { of } from "@tolki/str";

const result = of("Laravel").upper();

// result is a Stringable class instance representing "LARAVEL"

Released under the MIT License.