0
# String Functions
1
2
String manipulation utilities for case conversion, trimming, templating, and text processing operations.
3
4
## Capabilities
5
6
### Case Conversion
7
8
Functions for converting string case and capitalization.
9
10
```javascript { .api }
11
/**
12
* Converts string to camel case
13
* @param {string} str - The string to convert
14
* @returns {string} Returns the camel cased string
15
*/
16
function camelCase(str);
17
18
/**
19
* Converts string to kebab case
20
* @param {string} str - The string to convert
21
* @returns {string} Returns the kebab cased string
22
*/
23
function kebabCase(str);
24
25
/**
26
* Converts string to snake case
27
* @param {string} str - The string to convert
28
* @returns {string} Returns the snake cased string
29
*/
30
function snakeCase(str);
31
32
/**
33
* Converts string to start case
34
* @param {string} str - The string to convert
35
* @returns {string} Returns the start cased string
36
*/
37
function startCase(str);
38
39
/**
40
* Converts string to lower case
41
* @param {string} str - The string to convert
42
* @returns {string} Returns the lower cased string
43
*/
44
function lowerCase(str);
45
46
/**
47
* Converts string to upper case
48
* @param {string} str - The string to convert
49
* @returns {string} Returns the upper cased string
50
*/
51
function upperCase(str);
52
53
/**
54
* Converts the first character of string to upper case and the remaining to lower case
55
* @param {string} str - The string to capitalize
56
* @returns {string} Returns the capitalized string
57
*/
58
function capitalize(str);
59
60
/**
61
* Converts the first character of string to lower case
62
* @param {string} str - The string to convert
63
* @returns {string} Returns the converted string
64
*/
65
function lowerFirst(str);
66
67
/**
68
* Converts the first character of string to upper case
69
* @param {string} str - The string to convert
70
* @returns {string} Returns the converted string
71
*/
72
function upperFirst(str);
73
74
/**
75
* Converts string, as space separated words, to lower case
76
* @param {string} str - The string to convert
77
* @returns {string} Returns the lower cased string
78
*/
79
function toLower(str);
80
81
/**
82
* Converts string, as space separated words, to upper case
83
* @param {string} str - The string to convert
84
* @returns {string} Returns the upper cased string
85
*/
86
function toUpper(str);
87
```
88
89
### String Trimming and Padding
90
91
Functions for trimming whitespace and padding strings.
92
93
```javascript { .api }
94
/**
95
* Removes leading and trailing whitespace or specified characters from string
96
* @param {string} str - The string to trim
97
* @param {string} chars - The characters to trim
98
* @returns {string} Returns the trimmed string
99
*/
100
function trim(str, chars);
101
102
/**
103
* Removes trailing whitespace or specified characters from string
104
* @param {string} str - The string to trim
105
* @param {string} chars - The characters to trim
106
* @returns {string} Returns the trimmed string
107
*/
108
function trimEnd(str, chars);
109
110
/**
111
* Removes leading whitespace or specified characters from string
112
* @param {string} str - The string to trim
113
* @param {string} chars - The characters to trim
114
* @returns {string} Returns the trimmed string
115
*/
116
function trimStart(str, chars);
117
118
/**
119
* Pads string on the left and right sides if it's shorter than length
120
* @param {string} str - The string to pad
121
* @param {number} length - The padding length
122
* @param {string} chars - The string used as padding
123
* @returns {string} Returns the padded string
124
*/
125
function pad(str, length, chars);
126
127
/**
128
* Pads string on the right side if it's shorter than length
129
* @param {string} str - The string to pad
130
* @param {number} length - The padding length
131
* @param {string} chars - The string used as padding
132
* @returns {string} Returns the padded string
133
*/
134
function padEnd(str, length, chars);
135
136
/**
137
* Pads string on the left side if it's shorter than length
138
* @param {string} str - The string to pad
139
* @param {number} length - The padding length
140
* @param {string} chars - The string used as padding
141
* @returns {string} Returns the padded string
142
*/
143
function padStart(str, length, chars);
144
```
145
146
### String Testing
147
148
Functions for testing string content and boundaries.
149
150
```javascript { .api }
151
/**
152
* Checks if string ends with the given target string
153
* @param {string} str - The string to inspect
154
* @param {string} target - The string to search for
155
* @param {number} position - The position to search up to
156
* @returns {boolean} Returns true if string ends with target, else false
157
*/
158
function endsWith(str, target, position);
159
160
/**
161
* Checks if string starts with the given target string
162
* @param {string} str - The string to inspect
163
* @param {string} target - The string to search for
164
* @param {number} position - The position to search from
165
* @returns {boolean} Returns true if string starts with target, else false
166
*/
167
function startsWith(str, target, position);
168
```
169
170
### String Escaping and Unescaping
171
172
Functions for escaping and unescaping special characters.
173
174
```javascript { .api }
175
/**
176
* Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities
177
* @param {string} str - The string to escape
178
* @returns {string} Returns the escaped string
179
*/
180
function escape(str);
181
182
/**
183
* The inverse of escape; converts HTML entities back to characters
184
* @param {string} str - The string to unescape
185
* @returns {string} Returns the unescaped string
186
*/
187
function unescape(str);
188
189
/**
190
* Escapes the RegExp special characters "^", "$", "\\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string
191
* @param {string} str - The string to escape
192
* @returns {string} Returns the escaped string
193
*/
194
function escapeRegExp(str);
195
```
196
197
### String Manipulation
198
199
Functions for modifying and transforming strings.
200
201
```javascript { .api }
202
/**
203
* Deburrs string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters
204
* @param {string} str - The string to deburr
205
* @returns {string} Returns the deburred string
206
*/
207
function deburr(str);
208
209
/**
210
* Repeats the given string n times
211
* @param {string} str - The string to repeat
212
* @param {number} n - The number of times to repeat the string
213
* @returns {string} Returns the repeated string
214
*/
215
function repeat(str, n);
216
217
/**
218
* Replaces matches for pattern in string with replacement
219
* @param {string} str - The string to modify
220
* @param {RegExp|string} pattern - The pattern to replace
221
* @param {Function|string} replacement - The match replacement
222
* @returns {string} Returns the modified string
223
*/
224
function replace(str, pattern, replacement);
225
226
/**
227
* Splits string by separator
228
* @param {string} str - The string to split
229
* @param {RegExp|string} separator - The separator pattern to split by
230
* @param {number} limit - The length to truncate results to
231
* @returns {Array} Returns the string segments
232
*/
233
function split(str, separator, limit);
234
235
/**
236
* Truncates string if it's longer than the given maximum string length
237
* @param {string} str - The string to truncate
238
* @param {Object} options - The options object
239
* @param {number} options.length - The maximum string length
240
* @param {string} options.omission - The string to indicate text is omitted
241
* @param {RegExp|string} options.separator - The separator pattern to truncate to
242
* @returns {string} Returns the truncated string
243
*/
244
function truncate(str, options);
245
246
/**
247
* Splits string into an array of its words
248
* @param {string} str - The string to inspect
249
* @param {RegExp|string} pattern - The pattern to match words
250
* @returns {Array} Returns the words of string
251
*/
252
function words(str, pattern);
253
```
254
255
### String Conversion
256
257
Functions for converting strings to other types.
258
259
```javascript { .api }
260
/**
261
* Converts string to an integer of the specified radix
262
* @param {string} str - The string to convert
263
* @param {number} radix - The radix to interpret the value by
264
* @returns {number} Returns the converted integer
265
*/
266
function parseInt(str, radix);
267
```
268
269
### String Templating
270
271
Advanced templating functionality for dynamic string generation.
272
273
```javascript { .api }
274
/**
275
* Creates a compiled template function that can interpolate data properties in "interpolate" delimiters
276
* @param {string} str - The template string
277
* @param {Object} options - The options object
278
* @param {RegExp} options.escape - The HTML "escape" delimiter
279
* @param {RegExp} options.evaluate - The "evaluate" delimiter
280
* @param {Object} options.imports - An object to import into the template as free variables
281
* @param {RegExp} options.interpolate - The "interpolate" delimiter
282
* @param {string} options.sourceURL - The sourceURL of the template's compiled source
283
* @param {string} options.variable - The data object variable name
284
* @returns {Function} Returns the compiled template function
285
*/
286
function template(str, options);
287
```
288
289
### Template Settings
290
291
Configuration object for template function behavior.
292
293
```javascript { .api }
294
/**
295
* Template settings configuration object
296
* @type {Object}
297
* @property {RegExp} escape - Used to detect data property values to be HTML-escaped
298
* @property {RegExp} evaluate - Used to detect code to be evaluated
299
* @property {RegExp} interpolate - Used to detect data property values to inject
300
* @property {string} variable - Used to reference the data object in the template text
301
* @property {Object} imports - Used to import variables into the compiled template
302
*/
303
const templateSettings;
304
```
305
306
## Usage Examples
307
308
### Case Conversion
309
310
```javascript
311
import { camelCase, kebabCase, snakeCase, startCase, capitalize } from "lodash-es";
312
313
const text = "hello world example";
314
315
// Different case conversions
316
const camelCased = camelCase(text); // "helloWorldExample"
317
const kebabCased = kebabCase(text); // "hello-world-example"
318
const snakeCased = snakeCase(text); // "hello_world_example"
319
const startCased = startCase(text); // "Hello World Example"
320
const capitalized = capitalize(text); // "Hello world example"
321
322
// Converting mixed case strings
323
const mixedCase = "XMLHttpRequest";
324
const kebab = kebabCase(mixedCase); // "xml-http-request"
325
const snake = snakeCase(mixedCase); // "xml_http_request"
326
```
327
328
### String Trimming and Padding
329
330
```javascript
331
import { trim, trimStart, trimEnd, pad, padStart, padEnd } from "lodash-es";
332
333
const text = " hello world ";
334
const customTrim = "-_-hello-_-";
335
336
// Basic trimming
337
const trimmed = trim(text); // "hello world"
338
const trimmedStart = trimStart(text); // "hello world "
339
const trimmedEnd = trimEnd(text); // " hello world"
340
341
// Custom character trimming
342
const customTrimmed = trim(customTrim, "_-"); // "hello"
343
344
// Padding
345
const shortText = "hi";
346
const padded = pad(shortText, 10); // " hi "
347
const paddedStart = padStart(shortText, 5, "0"); // "000hi"
348
const paddedEnd = padEnd(shortText, 5, "!"); // "hi!!!"
349
```
350
351
### String Manipulation
352
353
```javascript
354
import { repeat, replace, split, truncate, words, deburr } from "lodash-es";
355
356
// Repeat string
357
const repeated = repeat("abc", 3); // "abcabcabc"
358
359
// Replace text
360
const text = "Hello world, wonderful world";
361
const replaced = replace(text, /world/g, "universe"); // "Hello universe, wonderful universe"
362
363
// Split string
364
const sentence = "apple,banana,cherry";
365
const fruits = split(sentence, ","); // ["apple", "banana", "cherry"]
366
const limited = split(sentence, ",", 2); // ["apple", "banana"]
367
368
// Truncate long text
369
const longText = "This is a very long sentence that needs to be truncated";
370
const truncated = truncate(longText, {
371
length: 30,
372
omission: "...",
373
separator: " "
374
}); // "This is a very long sentence..."
375
376
// Extract words
377
const wordsArray = words("hello world, how are you?"); // ["hello", "world", "how", "are", "you"]
378
379
// Remove accents
380
const accented = "déjà vu café";
381
const deburred = deburr(accented); // "deja vu cafe"
382
```
383
384
### String Testing and Boundaries
385
386
```javascript
387
import { startsWith, endsWith } from "lodash-es";
388
389
const filename = "document.pdf";
390
const url = "https://example.com/api/users";
391
392
// Test string boundaries
393
const isPdf = endsWith(filename, ".pdf"); // true
394
const isHttps = startsWith(url, "https://"); // true
395
const isApi = startsWith(url, "api", 20); // true (starts at position 20)
396
```
397
398
### String Escaping
399
400
```javascript
401
import { escape, unescape, escapeRegExp } from "lodash-es";
402
403
// HTML escaping
404
const html = '<div class="container">Hello & welcome</div>';
405
const escaped = escape(html); // "<div class="container">Hello & welcome</div>"
406
const unescaped = unescape(escaped); // original HTML
407
408
// RegExp escaping
409
const userInput = "How much $ do you have? (in USD)";
410
const regexSafe = escapeRegExp(userInput); // "How much \\$ do you have\\? \\(in USD\\)"
411
const regex = new RegExp(regexSafe); // Safe to use in regex
412
```
413
414
### Template Engine
415
416
```javascript
417
import { template, templateSettings } from "lodash-es";
418
419
// Basic templating
420
const compiled = template("Hello <%= name %>!");
421
const result = compiled({ name: "Alice" }); // "Hello Alice!"
422
423
// Advanced templating with logic
424
const advancedTemplate = template(`
425
<% if (users.length) { %>
426
<ul>
427
<% users.forEach(function(user) { %>
428
<li><%- user.name %> (<%- user.age %>)</li>
429
<% }); %>
430
</ul>
431
<% } else { %>
432
<p>No users found.</p>
433
<% } %>
434
`);
435
436
const users = [
437
{ name: "Alice", age: 25 },
438
{ name: "Bob", age: 30 }
439
];
440
441
const html = advancedTemplate({ users });
442
443
// Custom template settings
444
const customTemplate = template("Hello {{name}}!", {
445
interpolate: /\{\{([\s\S]+?)\}\}/g
446
});
447
const customResult = customTemplate({ name: "World" }); // "Hello World!"
448
```