import { codePointAt } from 'src/string';
const str = 'string';
const position = 2; // zero index based position
const result = codePointAt(str, position);
result === 114; // true
import { endsWith } from 'src/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
import { fromCodePoint } from 'src/string';
const result = fromCodePoint(97, 98, 99, 49, 50, 51);
result === 'abc123'; // true
import { includes } from 'src/string';
const str = 'string';
const search = 'ring';
const position = 0; // index to begin searching at
const result = includes(str, search, position);
result === true; // true
import { repeat } from 'src/string';
const str = 'string';
const times = 3; // the number of times to repeat the string
const result = repeat(str, times);
result === 'stringstringstring'; // true
import { startsWith } from 'src/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:
import { escapeRegExp } from 'src/string';
const str = 'cat file.js | grep -c';
const result = escapeRegExp(str);
result === 'cat file\\.js \\| grep -c'; // true
import { escapeXml } from 'src/string';
const badCode = "<script>alert('hi')</script>";
const sanitized = escapeXml(badCode);
sanitized === '<script>alert('hi')</script>'; // true
import { padEnd } from 'src/string';
const str = 'string';
const length = 10;
const char = '=';
const result = padEnd(str, length, char);
result === 'string===='; // true
import { padStart } from 'src/string';
const str = 'string';
const length = 10;
const char = '=';
const result = padStart(str, length, char);
result === '====string'; // true