numberFormat _.numberFormat(number, [ decimals=0, decimalSeparator='.', orderSeparator=','])
Formats the numbers.
_.numberFormat(1000, 2)
=> "1,000.00"
_.numberFormat(123456789.123, 5, '.', ',')
=> "123,456,789.12300"capitalize _.capitalize(string)
Converts first letter of the string to uppercase.
_.capitalize("foo Bar")
=> "Foo Bar"chop _.chop(string, step)
_.chop('whitespace', 3)
=> ['whi','tes','pac','e']clean _.clean(str)
Compress some whitespaces to one.
_.clean(" foo bar ")
=> 'foo bar'swapCase _.swapCase(str)
Returns a copy of the string in which all the case-based characters have had their case swapped.
_.swapCase('hELLO')
=> 'Hello'includes _.includes(string, substring)
Tests if string contains a substring.
_.includes("foobar", "ob")
=> trueincludes function was removed
But you can create it in this way, for compatibility with previous versions:
_.includes = _.str.includecount _.count(string, substring)
_('Hello world').count('l')
=> 3insert _.insert(string, index, substing)
_.insert('Hello ', 6, 'world')
=> 'Hello world'join _.join(separator, strings...)
Joins strings together with given separator
_.join(" ", "foo", "bar")
=> "foo bar"lines _.lines(str)
_.lines("Hello\nWorld")
=> ["Hello", "World"]reverse Return reversed string:
_.str.reverse("foobar")
=> 'raboof'splice _.splice(string, index, howmany, substring)
Like a array splice.
_('https://[email protected]/edtsech/underscore.strings').splice(30, 7, 'andre')
=> 'https://[email protected]/andre/underscore.strings'startsWith _.startsWith(string, starts)
This method checks whether string starts with starts.
_("image.gif").startsWith("image")
=> trueendsWith _.endsWith(string, ends)
This method checks whether string ends with ends.
_("image.gif").endsWith("gif")
=> truesucc _.succ(str)
Returns the successor to str.
_('a').succ()
=> 'b'
_('A').succ()
=> 'B'titleize _.titleize(string)
_('my name is andre').titleize()
=> 'My Name Is Andre'camelize _.camelize(string)
Converts underscored or dasherized string to a camelized one. Begins with a lower case letter unless it starts with an underscore or string
_('moz-transform').camelize()
=> 'mozTransform'
_('-moz-transform').camelize()
=> 'MozTransform'classify _.classify(string)
Converts string to camelized class name. First letter is always upper case
_('some_class_name').classify()
=> 'SomeClassName'underscored _.underscored(string)
Converts a camelized or dasherized string into an underscored one
_('MozTransform').underscored()
=> 'moz_transform'dasherize _.dasherize(string)
Converts a underscored or camelized string into an dasherized one
_('MozTransform').dasherize()
=> '-moz-transform'humanize _.humanize(string)
Converts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace, and removes the postfix '_id'.
_(' capitalize dash-CamelCase_underscore trim ').humanize()
=> 'Capitalize dash camel case underscore trim'trim _.trim(string, [characters])
trims defined characters from begining and ending of the string. Defaults to whitespace characters.
_.trim(" foobar ")
=> "foobar"
_.trim("_-foobar-_", "_-")
=> "foobar"ltrim _.ltrim(string, [characters])
Left trim. Similar to trim, but only for left side.
rtrim _.rtrim(string, [characters])
Right trim. Similar to trim, but only for right side.
truncate _.truncate(string, length, truncateString)
_('Hello world').truncate(5)
=> 'Hello...'
_('Hello').truncate(10)
=> 'Hello'prune _.prune(string, length, pruneString)
Elegant version of truncate. Makes sure the pruned string does not exceed the original length. Avoid half-chopped words when truncating.
_('Hello, world').prune(5)
=> 'Hello...'
_('Hello, world').prune(8)
=> 'Hello...'
_('Hello, world').prune(5, ' (read a lot more)')
=> 'Hello, world' (as adding "(read a lot more)" would be longer than the original string)
_('Hello, cruel world').prune(15)
=> 'Hello, cruel...'
_('Hello').prune(10)
=> 'Hello'words _.words(str, delimiter=/\s+/)
Split string by delimiter (String or RegExp), /\s+/ by default.
_.words(" I love you ")
=> ["I","love","you"]
_.words("I_love_you", "_")
=> ["I","love","you"]
_.words("I-love-you", /-/)
=> ["I","love","you"]
_.words(" ")
=> []sprintf _.sprintf(string format, *arguments)
C like string formatting. Credits goes to Alexandru Marasteanu. For more detailed documentation, see the original page.
_.sprintf("%.1f", 1.17)
"1.2"pad _.pad(str, length, [padStr, type])
pads the str with characters until the total string length is equal to the passed length parameter. By default, pads on the left with the space char (" "). padStr is truncated to a single character if necessary.
_.pad("1", 8)
-> " 1";
_.pad("1", 8, '0')
-> "00000001";
_.pad("1", 8, '0', 'right')
-> "10000000";
_.pad("1", 8, '0', 'both')
-> "00001000";
_.pad("1", 8, 'bleepblorp', 'both')
-> "bbbb1bbb";lpad _.lpad(str, length, [padStr])
left-pad a string. Alias for pad(str, length, padStr, 'left')
_.lpad("1", 8, '0')
-> "00000001";rpad _.rpad(str, length, [padStr])
right-pad a string. Alias for pad(str, length, padStr, 'right')
_.rpad("1", 8, '0')
-> "10000000";lrpad _.lrpad(str, length, [padStr])
left/right-pad a string. Alias for pad(str, length, padStr, 'both')
_.lrpad("1", 8, '0')
-> "00001000";center alias for lrpad
ljust alias for rpad
rjust alias for lpad
toNumber _.toNumber(string, [decimals])
Parse string to number. Returns NaN if string can't be parsed to number.
_('2.556').toNumber()
=> 3
_('2.556').toNumber(1)
=> 2.6strRight _.strRight(string, pattern)
Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern or all string if no match found.
_('This_is_a_test_string').strRight('_')
=> "is_a_test_string";strRightBack _.strRightBack(string, pattern)
Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern or all string if no match found.
_('This_is_a_test_string').strRightBack('_')
=> "string";strLeft _.strLeft(string, pattern)
Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern or all string if no match found.
_('This_is_a_test_string').strLeft('_')
=> "This";strLeftBack _.strLeftBack(string, pattern)
Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern or all string if no match found.
_('This_is_a_test_string').strLeftBack('_')
=> "This_is_a_test";stripTags
Removes all html tags from string.
_('a <a href="#">link</a>').stripTags()
=> 'a link'
_('a <a href="#">link</a><script>alert("hello world!")</script>').stripTags()
=> 'a linkalert("hello world!")'toSentence _.toSentence(array, [delimiter, lastDelimiter])
Join an array into a human readable sentence.
_.toSentence(['jQuery', 'Mootools', 'Prototype'])
=> 'jQuery, Mootools and Prototype';
_.toSentence(['jQuery', 'Mootools', 'Prototype'], ', ', ' unt ')
=> 'jQuery, Mootools unt Prototype';toSentenceSerial _.toSentenceSerial(array, [delimiter, lastDelimiter])
The same as toSentence, but adjusts delimeters to use Serial comma.
_.toSentenceSerial(['jQuery', 'Mootools'])
=> 'jQuery and Mootools';
_.toSentenceSerial(['jQuery', 'Mootools', 'Prototype'])
=> 'jQuery, Mootools, and Prototype'
_.toSentenceSerial(['jQuery', 'Mootools', 'Prototype'], ', ', ' unt ');
=> 'jQuery, Mootools, unt Prototype';repeat _.repeat(string, count, [separator])
Repeats a string count times.
_.repeat("foo", 3)
=> 'foofoofoo';
_.repeat("foo", 3, "bar")
=> 'foobarfoobarfoo'surround _.surround(string, wrap)
Surround a string with another string.
_.surround("foo", "ab")
=> 'abfooab';quote _.quote(string, quoteChar) or _.q(string, quoteChar)
Quotes a string. quoteChar defaults to ".
_.quote('foo', quoteChar)
=> '"foo"';unquote _.unquote(string, quoteChar)
Unquotes a string. quoteChar defaults to ".
_.unquote('"foo"')
=> 'foo';
_.unquote("'foo'", "'")
=> 'foo';slugify _.slugify(string)
Transform text into a URL slug. Replaces whitespaces, accentuated, and special characters with a dash.
_.slugify("Un éléphant à l'orée du bois")
=> 'un-elephant-a-loree-du-bois';naturalCmp array.sort(_.naturalCmp)
Naturally sort strings like humans would do.
['foo20', 'foo5'].sort(_.naturalCmp)
=> [ 'foo5', 'foo20' ]