Skip to content

Latest commit

 

History

History
126 lines (77 loc) · 2.68 KB

string.md

File metadata and controls

126 lines (77 loc) · 2.68 KB

string

codePointAt

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

endsWith

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

fromCodePoint

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

includes

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

repeat

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

startsWith

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:

padEnd

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

padStart

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