- Source:
the JS native String class
Methods
camelCase(s) → {string}
- Source:
camel cases a string
Example
eg. usage
console.log(String.camelCase('Foo Bar')); // 'fooBar'
console.log(String.camelCase('--foo-bar--')); // 'fooBar'
console.log(String.camelCase('__FOO_BAR__')); // 'fooBar'
console.log('Foo Bar'.camelCase()); // 'fooBar'
console.log('--foo-bar--'.camelCase()); // 'fooBar'
console.log('__FOO_BAR__'.camelCase()); // 'fooBar'
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string |
Returns:
- Type
- string
capitalize(s) → {string}
- Source:
capitalizes a string
Example
eg. usage
console.log(String.capitalize('we\'re happy to use flavorJS')); // Were Happy To Use Flavor JS
console.log(String.capitalize('we\'re happy to use flavorJS')); // We're happy to use flavorjs
console.log(String.capitalize('flavorJS')); // Flavorjs
Parameters:
Name | Type | Description |
---|---|---|
s |
string |
Returns:
- Type
- string
contains(s, value, insensitiveopt) → {boolean}
- Source:
checks if a string contains another string
Example
eg. usage
console.log(String.contains('FlavorJS is tasty', 'JS is')); // true
console.log(String.contains('FlavorJS is tasty', 'js is')); // false
console.log(String.contains('FlavorJS is tasty', 'js is', true)); // true
console.log(String.contains('flavorJS Is tasty', 'JS is', true)); // true
console.log('FlavorJS is tasty'.contains('JS is')); // true
console.log('FlavorJS is tasty'.contains('js is')); // false
console.log('FlavorJS is tasty'.contains('js is', true)); // true
console.log('flavorJS is tasty'.contains('JS is', true)); // true
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | the string to be checked | ||
value |
string | the string value to check | ||
insensitive |
boolean |
<optional> |
false
|
true if you want to do an insensitive check |
Returns:
- Type
- boolean
decodeURI(s) → {string}
- Source:
decodes an URI string
Example
eg. usage
console.log(String.decodeURI('https%3A%2F%2Fflavorjs.io%2Fpage.html%3Fname%3Dblack%20mirror%26email%3Dusername%40example.com')); // 'https://flavorjs.io/page.html?name=black mirror&email=username@example.com'
console.log(('https%3A%2F%2Fflavorjs.io%2Fpage.html%3Fname%3Dblack%20mirror%26email%3Dusername%40example.com').decodeURI()); // 'https://flavorjs.io/page.html?name=black mirror&email=username@example.com'
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the URI string |
Returns:
- Type
- string
encodeURI(s) → {string}
- Source:
encodes an URI string
Example
eg. usage
console.log(String.encodeURI('https://flavorjs.io/page.html?name=black mirror&email=username@example.com')); // 'https%3A%2F%2Fflavorjs.io%2Fpage.html%3Fname%3Dblack%20mirror%26email%3Dusername%40example.com'
console.log(('https://flavorjs.io/page.html?name=black mirror&email=username@example.com').encodeURI()); // 'https%3A%2F%2Fflavorjs.io%2Fpage.html%3Fname%3Dblack%20mirror%26email%3Dusername%40example.com'
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the URI string |
Returns:
- Type
- string
endsWith(s, value, insensitiveopt) → {boolean}
- Source:
checks if a string ends with another string
Example
eg. usage
console.log(String.endsWith('FlavorJS is tasty', 'Tasty')); // false
console.log(String.endsWith('FlavorJS is tasty', 'tasty')); // true
console.log(String.endsWith('FlavorJS is tasty', 'Tasty', true)); // true
console.log(String.endsWith('FlavorJS is Tasty', 'tasty', true)); // true
console.log(('FlavorJS is tasty').endsWith('Tasty')); // false
console.log(('FlavorJS is tasty').endsWith('tasty')); // true
console.log(('FlavorJS is tasty').endsWith('Tasty', true)); // true
console.log(('flavorJS is Tasty').endsWith('tasty', true)); // true
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | the string to be checked | ||
value |
string | the value to check | ||
insensitive |
boolean |
<optional> |
false
|
true if you want to do an insensitive check |
Returns:
- Type
- boolean
escapeHTML(s) → {string}
- Source:
unescapes an HTML code string
Example
eg. usage
console.log(String.unescapeHTML('<div>flavor & js = tasty</div>')); // '<div>flavor & js = tasty</div>'
console.log(('<div>flavor & js = tasty</div>').unescapeHTML()); // '<div>flavor & js = tasty</div>'
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the HTML code string |
Returns:
- Type
- string
escapeHTML(s) → {string}
- Source:
escapes an HTML code string
Example
eg. usage
console.log(String.escapeHTML('<div>flavor & js = tasty</div>')); // '<div>flavor & js = tasty</div>'
console.log(('<div>flavor & js = tasty</div>').escapeHTML()); // '<div>flavor & js = tasty</div>'
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the HTML code string |
Returns:
- Type
- string
extractDomain(s, level, excludeWww) → {*}
- Source:
extracts a domain from an URI string with level parameter
Example
eg. usage
console.log(String.extractDomain('http://www.google.com')); // google.com
console.log(String.extractDomain('http://www.google.com', )); // google.com
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the URI string |
level |
number | the domain level to extract, starting from right obviously |
excludeWww |
boolean | true if you want to exclude the www. from che extraction |
Returns:
- Type
- *
extractFileExtension(s) → {*}
- Source:
extracts the file extension from a filename/path string
Example
eg. usage
console.log(String.extractFileExtension('file.jpg')); // 'jpg'
console.log(String.extractFileExtension('file')); // ''
console.log(String.extractFileExtension(1)); // 1
console.log(('file.jpg').extractFileExtension()); // 'jpg'
console.log(('file').extractFileExtension()); // ''
console.log((1).extractFileExtension()); // throws error
Parameters:
Name | Type | Description |
---|---|---|
s |
Returns:
- Type
- *
extractQueryString(s) → {object}
- Source:
extracts the query string object from an URI string
Examples
<caption>eg. usage</caption>
eg. usage
console.log(String.extractQueryString('https://flavorjs.io/page.html?name=black mirror&email=username@example.com')); // {name: 'black mirror', email: 'username@example.com'}
console.log(('https://flavorjs.io/page.html?name=black mirror&email=username@example.com').extractQueryString()); // {name: 'black mirror', email: 'username@example.com'}
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the URI string |
Returns:
- Type
- object
guid()
- Source:
stubs a GUID
Example
eg. usage
console.log(String.guid()); // 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
isRoman(s) → {boolean}
- Source:
checks if a string is a roman number string
Example
eg. usage
console.log(String.isRoman('MCMLXXX')); // true
console.log(String.isRoman('50,25')); // false
console.log(String.isRoman(5)); // false
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string to be checked |
Returns:
- Type
- boolean
isString(s) → {boolean}
- Source:
checks if something is a string
Example
eg. usage
var s = 'foo';
console.log(String.isString(s)); // true
console.log(String.isString(2)); // false
console.log(String.isString('')); // true
console.log(String.isString(null)); // false
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string to be checked |
Returns:
- Type
- boolean
isUrl(s) → {*|boolean}
- Source:
checks if a string is an url string (URI)
Example
eg. usage
console.log(String.isUrl('http://www.google.it')); // true
console.log(String.isUrl('50,25')); // false
console.log(String.isUrl(5)); // false
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string to check |
Returns:
- Type
- * | boolean
pad(s, length, chars) → {string}
- Source:
pads string on the left and right sides if it's shorter than length. Padding characters are truncated if they can't be evenly divided by length.
Example
eg. usage
console.log(String.pad('5', 5)); // ' 5 '
console.log(String.pad('5', 5, '0')); // '00500'
console.log(String.pad(4, 5, '01')); // '01401'
console.log(String.pad(true, 5, '01')); // '1true'
console.log(String.pad(4, 5, '01')); // '01401'
console.log(String.pad(new Date(), 50, '--') // '-----Tue Apr 04 2017 17:54:40 GMT+0000 (CEST)-----'
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string to be padded |
length |
number | the string length you need |
chars |
string | the char/chars to be used to pad the string |
Returns:
- Type
- string
padLeft(s, length, chars) → {string}
- Source:
pads left a string
Example
eg. usage
console.log(String.padLeft('5', 4)); // ' 5'
console.log(String.padLeft('5', 4, '0')); // '0005'
console.log(String.padLeft('5', 5, '01')); // '01015'
console.log(String.padLeft(5, 4, '0')); // '0005'
console.log(String.padLeft(true, 5, '0')); // '0true'
console.log(String.padLeft(new Date(), 50, '--') // '----------Tue Apr 04 2017 17:54:40 GMT+0000 (CEST)'
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string to be padded |
length |
number | the string length you need |
chars |
string | the char/chars to be used to pad the string |
Returns:
- Type
- string
padRight(s, length, chars) → {string}
- Source:
pads right a string
Example
eg. usage
console.log(String.padRight('5', 4)); // '5 '
console.log(String.padRight('5', 4, '0')); // '5000'
console.log(String.padRight('5', 5, '01')); // '50101'
console.log(String.padRight(5, 4, '0')); // '5000'
console.log(String.padRight(true, 5, '0')); // 'true0'
console.log(String.padRight(new Date(), 50, '--') // 'Tue Apr 04 2017 17:54:40 GMT+0000 (CEST)----------'
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string to be padded |
length |
number | the string length you need |
chars |
string | the char/chars to be used to pad the string |
Returns:
- Type
- string
parsePercentage(s) → {number}
- Source:
parses a percentage string to a number
Example
eg. usage
console.log(String.parsePercentage('50,25%')); // 50.25
console.log(String.parsePercentage('50,25')); // '50,25'
console.log(String.parsePercentage(5)); // 5
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string to be parsed |
Returns:
- Type
- number
parsePercentage(s) → {boolean}
- Source:
checks if a string is a percentage string
Example
eg. usage
console.log(String.isPercentage('50,25%')); // true
console.log(String.isPercentage('50,25')); // false
console.log(String.isPercentage(5)); // false
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string to be checked |
Returns:
- Type
- boolean
replaceAll(haystack, needle, replacementopt, insensitiveopt) → {string}
- Source:
replaces all occurrences of the needle inside the haystack with replacement
Example
eg. usage
console.log(String.replaceAll('FlavorJS is really really tasty', 'really', '')); // 'FlavorJS is tasty'
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
haystack |
string | the haystack string | ||
needle |
string | the needle string | ||
replacement |
string |
<optional> |
''
|
the replacement string |
insensitive |
boolean |
<optional> |
false
|
true if you want to do an insensitive check |
Returns:
- Type
- string
slugify(s, dashedopt) → {string}
- Source:
slugifies any string
Example
eg. usage
console.log(String.slugify('it\'s so good to be FlavorJS')); // 'its-so-good-to-be-flavor-js'
console.log(String.slugify('it\'s so gòòd - to_be | FlavorJS')); // 'its-so-good-to-be-flavor-js'
console.log(String.slugify('it\'s so gòòd - to_be | FlavorJS', false)); // 'itssogoodtobeflavorjs'
console.log('it\'s so good to be FlavorJS'.slugify()); // 'its-so-good-to-be-flavor-js'
console.log('it\'s so gòòd - to_be | FlavorJS'.slugify()); // 'its-so-good-to-be-flavor-js'
console.log('it\'s so gòòd - to_be | FlavorJS'.slugify(false)); // 'itssogoodtobeflavorjs'
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | the string | ||
dashed |
boolean |
<optional> |
true
|
false if you don't want dashed in the resulting string |
Returns:
- Type
- string
startsWith(s, value, insensitiveopt) → {boolean}
- Source:
checks if a string starts with another string
Example
eg. usage
console.log(String.startsWith('FlavorJS is tasty', 'Flavor')); // true
console.log(String.startsWith('FlavorJS is tasty', 'flavor')); // false
console.log(String.startsWith('FlavorJS is tasty', 'flavor', true)); // true
console.log(String.startsWith('flavorJS is tasty', 'Flavor', true)); // true
console.log('FlavorJS is tasty'.startsWith('Flavor')); // true
console.log('FlavorJS is tasty'.startsWith('flavor')); // false
console.log('FlavorJS is tasty'.startsWith('flavor', true)); // true
console.log('flavorJS is tasty'.startsWith('Flavor', true)); // true
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | the string to be checked | ||
value |
string | the value to check | ||
insensitive |
boolean |
<optional> |
false
|
true if you want to do an insensitive check |
Returns:
- Type
- boolean
stripTags(s) → {string}
- Source:
strips all html tags from an html code string
Example
eg. usage
console.log(String.stripTags('<div>flavorJS</div>')); // 'flavorJS'
console.log(String.stripTags('flavorJS')); // 'flavorJS'
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string |
Returns:
- Type
- string
toArray(s, separatoropt, limitopt) → {array}
- Source:
transforms a string in an array of chars/words
Example
eg. usage
console.log(String.toArray('FlavorJS')); // ['F','l','a','v','o','r','J','S']
console.log(String.toArray('FlavorJS, is really, really tasty', ',')); // ['FlavorJS',' is really',' really tasty']
console.log(String.toArray('FlavorJS, is really, really tasty', ',', 2)); // ['FlavorJS',' is really']
console.log('FlavorJS'.toArray()); // ['F','l','a','v','o','r','J','S']
console.log('FlavorJS, is really, really tasty'.toArray(',')); // ['FlavorJS',' is really',' really tasty']
console.log('FlavorJS, is really, really tasty'.toArray(',', 2)); // ['FlavorJS',' is really']
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | the string | ||
separator |
string |
<optional> |
''
|
the separator to use for the splitting |
limit |
number |
<optional> |
null
|
the limit of items to extract starting from left |
Returns:
- Type
- array
toInt(s) → {number}
- Source:
converts a string to a number
Example
eg. usage
console.log(String.toInt('550')); // 550
Parameters:
Name | Type | Description |
---|---|---|
s |
string | the string |
Returns:
- Type
- number