Returns the UTF-16 encoded code point value of a position in a string.
import { codePointAt } from '@dojo/framework/shim/string';
const str = 'string';
const position = 2; // zero index based position
const result = codePointAt(str, position);
result === 114; // true
Determines whether a string ends with the given substring.
import { endsWith } from '@dojo/framework/shim/string';
const str = 'string';
const search = 'ing';
const endPosition = str.length; // the index the searching should stop before
const result = endsWith(str, search, endPosition);
result === true; // true
Creates a string using the specified sequence of code points.
import { fromCodePoint } from '@dojo/framework/shim/string';
const result = fromCodePoint(97, 98, 99, 49, 50, 51);
result === 'abc123'; // true
Determines whether a string includes the given substring.
import { includes } from '@dojo/framework/shim/string';
const str = 'string';
const search = 'ring';
const position = 0; // index to begin searching at
const result = includes(str, search, position);
result === true; // true
Returns a string containing a string repeated a given number of times.
import { repeat } from '@dojo/framework/shim/string';
const str = 'string';
const times = 3; // the number of times to repeat the string
const result = repeat(str, times);
result === 'stringstringstring'; // true
Determines whether a string begins with the given substring.
import { startsWith } from '@dojo/framework/shim/string';
const str = 'string';
const search = 'str';
const result = startsWith(str, search);
result === true; // true
Special thanks to Mathias Bynens for granting permission to adopt code from his codePointAt
, fromCodePoint
, and repeat
polyfills.
The string
module also contains the following utility functions:
Adds padding to the end of a string to ensure it is a certain length.
import { padEnd } from '@dojo/framework/shim/string';
const str = 'string';
const length = 10;
const char = '=';
const result = padEnd(str, length, char);
result === 'string===='; // true
Adds padding to the beginning of a string to ensure it is a certain length.
import { padStart } from '@dojo/framework/shim/string';
const str = 'string';
const length = 10;
const char = '=';
const result = padStart(str, length, char);
result === '====string'; // true