- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.6.x
- Description
- A modern JavaScript utility library delivering modularity, performance & extras
- Author
- tessl
- Last updated
string.md docs/
1# String Processing23String manipulation including case conversion, trimming, padding, and template utilities. These methods provide comprehensive string processing capabilities.45## Capabilities67### Case Conversion89Convert strings between different case formats.1011```javascript { .api }12/**13* Converts `string` to camel case.14*15* @param {string} [string=''] The string to convert16* @returns {string} Returns the camel cased string17*/18function camelCase(string?: string): string;1920/**21* Converts `string` to kebab case.22*23* @param {string} [string=''] The string to convert24* @returns {string} Returns the kebab cased string25*/26function kebabCase(string?: string): string;2728/**29* Converts `string` to snake case.30*31* @param {string} [string=''] The string to convert32* @returns {string} Returns the snake cased string33*/34function snakeCase(string?: string): string;3536/**37* Converts `string` to start case.38*39* @param {string} [string=''] The string to convert40* @returns {string} Returns the start cased string41*/42function startCase(string?: string): string;4344/**45* Converts the first character of `string` to upper case and the remaining to lower case.46*47* @param {string} [string=''] The string to capitalize48* @returns {string} Returns the capitalized string49*/50function capitalize(string?: string): string;5152/**53* Converts the first character of `string` to lower case.54*55* @param {string} [string=''] The string to convert56* @returns {string} Returns the converted string57*/58function lowerFirst(string?: string): string;5960/**61* Converts the first character of `string` to upper case.62*63* @param {string} [string=''] The string to convert64* @returns {string} Returns the converted string65*/66function upperFirst(string?: string): string;67```6869**Usage Examples:**7071```javascript72import { camelCase, kebabCase, snakeCase, startCase, capitalize } from "lodash";7374camelCase('hello world'); // 'helloWorld'75camelCase('--foo-bar--'); // 'fooBar'76camelCase('__FOO_BAR__'); // 'fooBar'7778kebabCase('helloWorld'); // 'hello-world'79kebabCase('hello world'); // 'hello-world'8081snakeCase('helloWorld'); // 'hello_world'82snakeCase('hello world'); // 'hello_world'8384startCase('helloWorld'); // 'Hello World'85startCase('--foo-bar--'); // 'Foo Bar'8687capitalize('HELLO'); // 'Hello'88capitalize('hello'); // 'Hello'89```9091### Case Transformation9293Transform entire strings to upper or lower case.9495```javascript { .api }96/**97* Converts `string`, as space separated words, to lower case.98*99* @param {string} [string=''] The string to convert100* @returns {string} Returns the lower cased string101*/102function lowerCase(string?: string): string;103104/**105* Converts `string`, as space separated words, to upper case.106*107* @param {string} [string=''] The string to convert108* @returns {string} Returns the upper cased string109*/110function upperCase(string?: string): string;111112/**113* Converts `string`, as a whole, to lower case.114*115* @param {string} [string=''] The string to convert116* @returns {string} Returns the lower cased string117*/118function toLower(string?: string): string;119120/**121* Converts `string`, as a whole, to upper case.122*123* @param {string} [string=''] The string to convert124* @returns {string} Returns the upper cased string125*/126function toUpper(string?: string): string;127```128129### String Padding130131Add padding to strings.132133```javascript { .api }134/**135* Pads `string` on the left and right sides if it's shorter than `length`.136* Padding characters are truncated if they can't be evenly divided by `length`.137*138* @param {string} [string=''] The string to pad139* @param {number} [length=0] The padding length140* @param {string} [chars=' '] The string used as padding141* @returns {string} Returns the padded string142*/143function pad(string?: string, length?: number, chars?: string): string;144145/**146* Pads `string` on the right side if it's shorter than `length`.147*148* @param {string} [string=''] The string to pad149* @param {number} [length=0] The padding length150* @param {string} [chars=' '] The string used as padding151* @returns {string} Returns the padded string152*/153function padEnd(string?: string, length?: number, chars?: string): string;154155/**156* Pads `string` on the left side if it's shorter than `length`.157*158* @param {string} [string=''] The string to pad159* @param {number} [length=0] The padding length160* @param {string} [chars=' '] The string used as padding161* @returns {string} Returns the padded string162*/163function padStart(string?: string, length?: number, chars?: string): string;164```165166**Usage Examples:**167168```javascript169import { pad, padEnd, padStart } from "lodash";170171pad('abc', 8); // ' abc '172pad('abc', 8, '_-'); // '_-abc_-_'173174padEnd('abc', 6); // 'abc '175padEnd('abc', 6, '_-'); // 'abc_-_'176177padStart('abc', 6); // ' abc'178padStart('abc', 6, '_-'); // '_-_abc'179```180181### String Trimming182183Remove whitespace or specified characters from strings.184185```javascript { .api }186/**187* Removes leading and trailing whitespace or specified characters from `string`.188*189* @param {string} [string=''] The string to trim190* @param {string} [chars=whitespace] The characters to trim191* @returns {string} Returns the trimmed string192*/193function trim(string?: string, chars?: string): string;194195/**196* Removes trailing whitespace or specified characters from `string`.197*198* @param {string} [string=''] The string to trim199* @param {string} [chars=whitespace] The characters to trim200* @returns {string} Returns the trimmed string201*/202function trimEnd(string?: string, chars?: string): string;203204/**205* Removes leading whitespace or specified characters from `string`.206*207* @param {string} [string=''] The string to trim208* @param {string} [chars=whitespace] The characters to trim209* @returns {string} Returns the trimmed string210*/211function trimStart(string?: string, chars?: string): string;212```213214### String Testing215216Test strings for specific patterns.217218```javascript { .api }219/**220* Checks if `string` starts with the given target string.221*222* @param {string} [string=''] The string to inspect223* @param {string} [target] The string to search for224* @param {number} [position=0] The position to search from225* @returns {boolean} Returns `true` if `string` starts with `target`, else `false`226*/227function startsWith(string?: string, target?: string, position?: number): boolean;228229/**230* Checks if `string` ends with the given target string.231*232* @param {string} [string=''] The string to inspect233* @param {string} [target] The string to search for234* @param {number} [position=string.length] The position to search up to235* @returns {boolean} Returns `true` if `string` ends with `target`, else `false`236*/237function endsWith(string?: string, target?: string, position?: number): boolean;238```239240### String Repetition241242Repeat strings multiple times.243244```javascript { .api }245/**246* Repeats the given string `n` times.247*248* @param {string} [string=''] The string to repeat249* @param {number} [n=1] The number of times to repeat the string250* @returns {string} Returns the repeated string251*/252function repeat(string?: string, n?: number): string;253```254255### String Replacement256257Replace parts of strings.258259```javascript { .api }260/**261* Replaces matches for `pattern` in `string` with `replacement`.262*263* @param {string} [string=''] The string to modify264* @param {RegExp|string} pattern The pattern to replace265* @param {Function|string} replacement The match replacement266* @returns {string} Returns the modified string267*/268function replace(string?: string, pattern: RegExp|string, replacement: Function|string): string;269```270271### String Truncation272273Truncate strings to specified lengths.274275```javascript { .api }276/**277* Truncates `string` if it's longer than the given maximum string length.278* The last characters of the truncated string are replaced with the omission string.279*280* @param {string} [string=''] The string to truncate281* @param {Object} [options] The options object282* @returns {string} Returns the truncated string283*/284function truncate(string?: string, options?: Object): string;285```286287**Usage Examples:**288289```javascript290import { truncate } from "lodash";291292truncate('hi-diddly-ho there, neighborino');293// 'hi-diddly-ho there, neighbo...'294295truncate('hi-diddly-ho there, neighborino', {296'length': 24,297'separator': ' '298});299// 'hi-diddly-ho there,...'300301truncate('hi-diddly-ho there, neighborino', {302'length': 24,303'separator': /,? +/304});305// 'hi-diddly-ho there...'306307truncate('hi-diddly-ho there, neighborino', {308'omission': ' [...]'309});310// 'hi-diddly-ho there, neig [...]'311```312313### String Splitting314315Split strings into arrays.316317```javascript { .api }318/**319* Splits `string` by `separator`.320*321* @param {string} [string=''] The string to split322* @param {RegExp|string} separator The separator pattern to split by323* @param {number} [limit] The length to truncate results to324* @returns {Array} Returns the string segments325*/326function split(string?: string, separator: RegExp|string, limit?: number): Array;327328/**329* Splits `string` into an array of its words.330*331* @param {string} [string=''] The string to inspect332* @param {RegExp|string} [pattern] The pattern to match words333* @returns {Array} Returns the words of `string`334*/335function words(string?: string, pattern?: RegExp|string): Array;336```337338### String Encoding339340Encode and decode strings for safety.341342```javascript { .api }343/**344* Converts the characters "&", "<", ">", '"', and "'" in `string` to their345* corresponding HTML entities.346*347* @param {string} [string=''] The string to escape348* @returns {string} Returns the escaped string349*/350function escape(string?: string): string;351352/**353* The inverse of `escape`; this method converts the HTML entities354* `&`, `<`, `>`, `"`, and `'` in `string` to355* their corresponding characters.356*357* @param {string} [string=''] The string to unescape358* @returns {string} Returns the unescaped string359*/360function unescape(string?: string): string;361362/**363* Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",364* "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.365*366* @param {string} [string=''] The string to escape367* @returns {string} Returns the escaped string368*/369function escapeRegExp(string?: string): string;370```371372### String Cleaning373374Clean and normalize strings.375376```javascript { .api }377/**378* Deburrs `string` by converting Latin-1 Supplement and Latin Extended-A379* letters to basic Latin letters and removing combining diacritical marks.380*381* @param {string} [string=''] The string to deburr382* @returns {string} Returns the deburred string383*/384function deburr(string?: string): string;385```386387**Usage Examples:**388389```javascript390import { deburr, escape, unescape } from "lodash";391392deburr('déjà vu'); // 'deja vu'393394escape('fred, barney, & pebbles');395// 'fred, barney, & pebbles'396397unescape('fred, barney, & pebbles');398// 'fred, barney, & pebbles'399```400401### String Parsing402403Parse strings into other types.404405```javascript { .api }406/**407* Converts `string` to an integer of the specified radix. If `radix` is408* `undefined` or `0`, a `radix` of `10` is used unless `value` is a409* hexadecimal, in which case a `radix` of `16` is used.410*411* @param {string} string The string to convert412* @param {number} [radix=10] The radix to interpret `value` by413* @returns {number} Returns the converted integer414*/415function parseInt(string: string, radix?: number): number;416```417418### Template Processing419420Create and process string templates.421422```javascript { .api }423/**424* Creates a compiled template function that can interpolate data properties425* in "interpolate" delimiters, HTML-escape interpolated data properties in426* "escape" delimiters, and execute JavaScript in "evaluate" delimiters.427*428* @param {string} [string=''] The template string429* @param {Object} [options] The options object430* @returns {Function} Returns the compiled template function431*/432function template(string?: string, options?: Object): Function;433```434435**Usage Examples:**436437```javascript438import { template } from "lodash";439440// Basic interpolation441const compiled = template('hello <%= user %>!');442compiled({ 'user': 'fred' }); // 'hello fred!'443444// Using "escape" delimiter to escape data445const compiled2 = template('<b><%- value %></b>');446compiled2({ 'value': '<script>' }); // '<b><script></b>'447448// Using "evaluate" delimiter to execute JavaScript449const compiled3 = template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');450compiled3({ 'users': ['fred', 'barney'] }); // '<li>fred</li><li>barney</li>'451452// Custom template settings453template.templateSettings = {454'interpolate': /{{([\s\S]+?)}}/g455};456457const compiled4 = template('hello {{ user }}!');458compiled4({ 'user': 'mustache' }); // 'hello mustache!'459```