String
The String
class provides a set of methods and properties for working with strings.
Installation
Section titled “Installation”Metro UI already includes the String
library as global class Str
and fabric method str()
.
You can use it without any additional installation, but if you want to use it in your project, you can install it with package manager:
npm install @olton/string
pnpm install @olton/string
yarn add @olton/string
import { Str, str } from "@olton/string";
also, you can use import
statement to import the specific string functions you need:
import { camelCase, capitalize } from "@olton/string";
console.log(camelCase("hello world")); // helloWorldconsole.log(capitalize("hello world")); // Hello world
or use CDN:
<script type="module"> import { Str, str } from "https://esm.run/@olton/string";</script>
Constructor
Section titled “Constructor”The Str
class is a wrapper around the native String
object, but provides an additional methods and properties for working with strings.
With the Str
you can create mutable or immutable strings.
Mutable strings default
Section titled “Mutable strings ”To create a mutable string, you can use the str()
function or Str
class with the mutable
option set to true
.
Mutable
string is a default type of string, created with class Str
.
const str1 = new Str('Hello, World!', {mutable: true}); // immutable stringconst str2 = str('Hello, World!', true); // mutable string
Immutable strings
Section titled “Immutable strings”To create an immutable string, you can use the str()
function or Str
class with the mutable
option set to false
.
const str1 = new Str('Hello, World!', {mutable: false}); // immutable stringconst str2 = str('Hello, World!', false); // immutable string
Primitive
Section titled “Primitive”You can use class Str
as a primitive type. By default it is string
, but you be used as a Numeric
with hint number
.
const str1 = new Str('100');console.log(""+str1); // "100"
const str2 = str('100');console.log(+str2); // 100
Properties
Section titled “Properties”length
Section titled “length”Property, returns the length of the string length
.
const str = str('Hello, World!');console.log( str.length ); // 13
Symbol.toStringTag
Section titled “Symbol.toStringTag”Return Str
.
Methods
Section titled “Methods”Set or get the value of the object Str
.
const str = str('Hello, World!');console.log( str.val() ); // Hello, World!
str.val("New value")console.log( str.val() ); // New value
append()
Section titled “append()”Appends a string to the end of the current string append(str, times = 1)
.
const str = str('Hello');str.append(', World!'); // Hello, World!
const str2 = str('More');str.append('.', 3); // More...
camelCase()
Section titled “camelCase()”Converts a string to camelCase camelCase()
.
const str = str('Hello, World!');str.camelCase(); // helloWorld
capitalize()
Section titled “capitalize()”Capitalizes the first letter of a string capitalize()
.
const str = str('hello, world!');str.capitalize(); // Hello, world!
chars()
Section titled “chars()”Returns an array of characters in the string: chars(str, ignore = [])
.
const str = str('Hello', ['o']);str.chars(); // ['H', 'e', 'l', 'l']
count()
Section titled “count()”Counts the number of chars in the string count()
.
const str = str('Hello, World!');str.count(); // 13
countChars()
Section titled “countChars()”Counts the number of chars in the string countChars(str, ignore = [])
.
const str = str('Hello, World!');str.countChars(['l']); // 10
countUniqueChars()
Section titled “countUniqueChars()”Counts the number of unique chars in the string countUniqueChars(str, ignore = [])
.
const str = str('Hello, World!');str.countUniqueChars(); // 10
countSubstr()
Section titled “countSubstr()”Counts the number of substrings in the string countSubstr(substr, ignore = [])
.
const str = str('Lorem ipsum dolor sit amet.');str.countSubstr('or'); // 2
countWords()
Section titled “countWords()”Counts the number of words in the string countWords()
.
const str = str('Lorem ipsum dolor sit amet.');str.countWords(); // 5
countUniqueWords()
Section titled “countUniqueWords()”Counts the number of unique words in the string countUniqueWords()
.
const str = str('Lorem ipsum dolor sit amet.');str.countUniqueWords(); // 5
dashedName()
Section titled “dashedName()”Converts a string to dashed name dashedName()
.
const str = str('Hello, World!');str.dashedName(); // hello-world
decapitalize()
Section titled “decapitalize()”Decapitalizes the first letter of a string decapitalize()
.
const str = str('Hello, World!');str.decapitalize(); // hello, World!
endsWith()
Section titled “endsWith()”Checks if the string ends with a substring endsWith(substr)
.
const str = str('Hello, World!');str.endsWith('World!'); // true
escapeHtml()
Section titled “escapeHtml()”Escapes HTML characters in the string escapeHtml()
.
const str = str('<div>Hello, World!</div>');str.escapeHtml(); // <div>Hello, World!</div>
first()
Section titled “first()”Get N
first chars of the string first(n)
.
const str = str('Hello, World!');str.first(5); // Hello
includes()
Section titled “includes()”Checks if the string includes a substring includes(substr)
.
const str = str('Hello, World!');str.includes('World!'); // true
insert()
Section titled “insert()”Inserts a string at the specified position insert(substr, pos)
.
const str = str('Hello, World!');str.insert('Beautiful ', 7); // Hello, Beautiful World!
isAlpha()
Section titled “isAlpha()”Checks if the string contains only letters isAlpha()
.
const str = str('Hello, World!');str.isAlpha(); // false
isAlphaDigit()
, isAlphaNum()
Section titled “isAlphaDigit(), isAlphaNum()”Checks if the string contains only letters and digits isAlphaDigit()
.
const str = str('Hello, World!');str.isAlphaDigit(); // false
const str = str('Hello5');str.isAlphaDigit(); // true
isBlank()
Section titled “isBlank()”Checks if the string is blank isBlank(strong)
.
const str = str('Hello, World!');str.isBlank(); // falseconst str = str(' ');str.isBlank(); // trueconst str = str(' ');str.isBlank(false); // false
isDigit()
Section titled “isDigit()”Checks if the string contains only digits isDigit()
.
const str = str('Hello, World!');str.isDigit(); // falseconst str = str('12345');str.isDigit(); // true
isEmpty()
Section titled “isEmpty()”Checks if the string is empty isEmpty()
.
const str = str('Hello, World!');str.isEmpty(); // falseconst str = str('');str.isEmpty(); // trueconst str = str(' ');str.isEmpty(); // true
isLower()
Section titled “isLower()”Checks if the string is lower case isLower()
.
const str = str('Hello, World!');str.isLower(); // falseconst str = str('hello, world!');str.isLower(); // true
isString()
Section titled “isString()”Checks if the string is a string isString()
.
const str = str('Hello, World!');str.isString(); // trueconst str = str(123);str.isString(); // false
isUpper()
Section titled “isUpper()”Checks if the string is upper case isUpper()
.
const str = str('Hello, World!');str.isUpper(); // falseconst str = str('HELLO, WORLD!');str.isUpper(); // true
kebabCase()
Section titled “kebabCase()”Converts a string to kebab-case kebabCase()
.
const str = str('Hello, World!');str.kebabCase(); // hello-world
last()
Section titled “last()”Get N
last chars of the string last(n)
.
const str = str('Hello, World!');str.last(6); // World!
lower()
Section titled “lower()”Converts a string to lower case lower()
.
const str = str('Hello, World!');str.lower(); // hello, world!
matches()
Section titled “matches()”Checks if the string matches a regular expression matches(regexp, flags = '')
.
const str = str('Hello, World!');str.matches(/Hello/); // true
Pads the string with a character to the left or right pad(char = ' ', len)
.
const str = str('Hello');str.pad(' ', 5); // ' Hello '
lpad()
Section titled “lpad()”Pads the string with a character to the left lpad(char = ' ', len)
.
const str = str('Hello');str.lpad(' ', 5); // ' Hello'
rpad()
Section titled “rpad()”Pads the string with a character to the right rpad(char = ' ', len)
.
const str = str('Hello');str.rpad(' ', 5); // 'Hello '
prepend()
Section titled “prepend()”Prepends a string to the beginning of the current string prepend(str)
.
const str = str('World!');str.prepend('Hello, '); // Hello, World!
prune()
Section titled “prune()”Truncates subject
to a new length
and doesn’t break the words with specified ending. prune(len, end = '...')
.
const str = str('Hello, World!');str.prune(5); // Hello...
repeat()
Section titled “repeat()”Repeats the string repeat(times)
.
const str = str('Hello');str.repeat(3); // HelloHelloHello
reverse()
Section titled “reverse()”Reverses the string reverse()
.
const str = str('Hello, World!');str.reverse(); // !dlroW ,olleH
shorten()
Section titled “shorten()”Shortens the string to a specified length shorten(len, end = '...')
.
const str = str('Hello, World!');str.shorten(5); // Hello...
shuffle()
Section titled “shuffle()”Shuffles the string shuffle()
.
const str = str('Hello, World!');str.shuffle(); // !dlroW ,olleH
slice()
Section titled “slice()”Slices the string to N
parts slice(parts)
.
const str = str('Hello World');str.slice(2); // ["Hello", "World"]
snakeCase()
Section titled “snakeCase()”Converts a string to snake_case snakeCase()
.
const str = str('Hello, World!');str.snakeCase(); // hello_world
split()
Section titled “split()”Splits the string into an array of strings split(separator, limit, trim)
.
const str = str('Hello, World!');str.split(','); // ['Hello', ' World!']str.split(',', undefined, true); // ['Hello', 'World!']str.split(',', 1, true); // ['Hello']
sprintf()
Section titled “sprintf()”vsprintf()
Section titled “vsprintf()”startsWith()
Section titled “startsWith()”Checks if the string starts with a substring startsWith(substr)
.
const str = str('Hello, World!');str.startsWith('Hello'); // true
strip()
Section titled “strip()”Strips the string of HTML tags strip(search, replace = '')
.
const str = str('Hello, World!');str.strip("Hello,", "New"); // New World!
stripBom()
Section titled “stripBom()”Strips the BOM (Byte Order Mark) from the string stripBom()
.
const str = str('\uFEFFHello, World!');str.stripBom(); // Hello, World!
stripTags()
Section titled “stripTags()”Strips the string of HTML tags stripTags(allowed = [])
.
const str = str('<div>Hello, <b>World!</b></div>');str.stripTags(); // Hello, World!str.stripTags(['b']); // <b>Hello, World!</b>
stripTagsAll()
Section titled “stripTagsAll()”Strips the string of HTML tags stripTagsAll()
.
const str = str('<div>Hello, <b>World!</b></div>');str.stripTagsAll(); // Hello, World!
substring()
Section titled “substring()”Returns a substring of the string substring(start, len)
.
const str = str('Hello, World!');str.substring(0, 5); // Hello
swap()
Section titled “swap()”Swaps the case of the string swap()
.
const str = str('Hello, World!');str.swap(); // hELLO, wORLD!
title()
Section titled “title()”Converts a string to title case, optionally allowing customization for splitting and ignoring certain characters. title(noSplit, sep = "")
.
noSplit
- string | string[] | boolean. A string, an array of characters, or a boolean indicating the characters to skip capitalization.sep
- string The separator to split thenoSplit
string (ifnoSplit
is a string).
const str = str('Hello, World!');str.title();
trim()
Section titled “trim()”Trims the string of whitespace trim()
.
const str = str(' Hello, World! ');str.trim(); // Hello, World!
ltrim()
Section titled “ltrim()”Trims the string of whitespace from the left ltrim()
.
const str = str(' Hello, World! ');str.ltrim(); // 'Hello, World! '
rtrim()
Section titled “rtrim()”Trims the string of whitespace from the right rtrim()
.
const str = str(' Hello, World! ');str.rtrim(); // ' Hello, World!'
truncate()
Section titled “truncate()”Truncates the string to a specified length truncate(len, end = '...')
.
const str = str('Hello, World!');str.truncate(5); // Hello...
truncateWithAlign()
Section titled “truncateWithAlign()”Truncates the string to a specified length and aligns it with the specified character truncateWithAlign(len, char = ' ', end = '...')
.
const str = str('Hello, World!');str.truncateWithAlign(5); // Hello...str.truncateWithAlign(5, '-'); // Hello-
unescapeHtml()
Section titled “unescapeHtml()”Unescapes HTML characters in the string unescapeHtml()
.
const str = str('<div>Hello, World!</div>');str.unescapeHtml(); // <div>Hello, World!</div>
unique()
Section titled “unique()”Removes duplicate characters from the string unique()
.
const str = str('Hello, World!');str.unique(); // Helo, Wrd!
uniqueWords()
Section titled “uniqueWords()”Removes duplicate words from the string uniqueWords()
.
const str = str('Hello, World! Hello, World!');str.uniqueWords(); // Hello, World!
upper()
Section titled “upper()”Converts a string to upper case upper()
.
const str = str('Hello, World!');str.upper(); // HELLO, WORLD!
words()
Section titled “words()”Returns an array of words in the string, you can set specified patter to split words(pattern, flags)
.
const str = str('Hello, World!');str.words(); // ['Hello', 'World!']
wrap()
Section titled “wrap()”Wraps the string with a specified character wrap(before, after)
.
const str = str('Hello, World!');str.wrap('<b>', '</b>'); // <b>Hello, World!</b>
wrapTag()
Section titled “wrapTag()”Wraps the string with a specified HTML tag wrapTag(tag, attr = {})
.
const str = str('Hello, World!');str.wrapTag('b', {class: 'bold'}); // <b class="bold">Hello, World!</b>
Returns the first non-null value from the list of arguments nvl(val, ...args)
.
const str = str('Hello, World!');str.nvl('Hello', 'World!'); // Hellostr.nvl(null, 'World!'); // Worldstr.nvl(null, null, 'Third!'); // Third