Skip to content
String manipulation extensions for Underscore.js javascript library.
JavaScript Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Idea: Esa-Matti Suuronen (esa-matti aet suuronen dot org)

Authors: Esa-Matti Suuronen, Edward Tsech

Javascript lacks complete string manipulation operations. This an attempt to fill that cap. List of buildin methods can be found for example from Dive Into JavaScript.

As name states this an extension for Underscore.js, but it can be used independently from _s-global variable. But with Underscore.js you can use Object-Oriented style and chaining:

_("   epeli  ").chain().trim().capitalize().value()
=> "Epeli"

String Functions

blank _.blank(string)

Checks if the string is empty or contains whitespaces only.

_.blank(' ')
=> true

capitalize _.capitalize(string)

Converts first letter of the string to uppercase.

=> "Epeli"

chop _.chop(string, step)

_.chop('whitespace', 3)
=> ['whi','tes','pac','e']

clean _.clean(str)

Compress some whitespaces to one.

_.clean(" foo    bar   ")
=> 'foo bar'

contains _.contains(string, substring)

Tests if string contains a substring.

_.contains("foobar", "ob")
=> true

count _.count(string, substring)

_('Hello world').count('l')
=> 3

empty _.empty(string)

=> true

escapeHTML _.escapeHTML(string)

Converts HTML special characters to their entity equivalents.

_('<div>Blah blah blah</div>').escapeHTML();
=> '&lt;div&gt;Blah blah blah&lt;/div&gt;'

includes alias for contains.

insert _.insert(string, index, substing)

_('Hello ').insert(6, 'world')
=> 'Hello world'

join _.join(separator, *strings)

Joins strings together with given separator

_.join(" ", "foo", "bar")
=> "foo bar"

reverse _.reverse(string)

Reverses the string.

=> "raboof"

splice _.splice(string, index, howmany, substring)

Like a array splice.

_('').splice(30, 7, 'epeli')
=> ''

startsWith _.startsWith(string, starts)

This method checks whether string starts with starts.

=> true

endsWith _.endsWith(string, ends)

This method checks whether string ends with ends.

=> true

succ _.succ(str)

Returns the successor to str.

=> 'b'

=> 'B'


Supplant function was removed, use Underscore.js template function.

strip alias for trim

lstrip alias for ltrim

rstrip alias for rtrim

titleize _.titleize(string)

_('my name is epeli').titleize()
=> 'My Name Is Epeli'

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])

Left trim. Similar to trim, but only for right side.

truncate _.truncate(string, length, truncateString)

_('Hello world').truncate(5)
=> 'Hello...'

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)

Any suggestions or bug reports are welcome. Just email me or more preferably open an issue.

Something went wrong with that request. Please try again.