This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Underscore.string Cheatsheet

Quick reference guide for underscore.string JavaScript library, covering case conversion, padding, trimming, sanitization, and number formatting.

· Published

underscore.string provides string manipulation extensions for JavaScript.

Case and Casing Conversion

s.capitalize("foo bar")       //=> "Foo bar"
s.humanize("hey-there foo")   //=> "Hey there foo"
s.titleize('my name is hi')   //=> "My Name Is Hi"

s.dasherize('MozTransform')   //=> "-moz-transform"
s.underscored('MozTransform') //=> "moz_transform"
s.classify('-moz-transform')  //=> "MozTransform"
s.camelize('moz_transform')   //=> "mozTransform"

s.slugify("hey there")        //=> "hey-there"
s.swapCase("hELLO")           //=> "Hello"

String Inspections

s.startsWith('image.gif', 'image') //=> true
s.endsWith('image.gif', '.gif')    //=> true
s.isBlank("   ")                   //=> true

HTML Entities and Stripping

s.escapeHTML("<div>")       //=> "&lt;div&gt;"
s.unescapeHTML("&lt;div&gt;") //=> "<div>"
s.stripTags("<div>hi</div>")  //=> "hi"

Quoting and Splitting

s.quote("hi", '"')    //=> '"hi"'
s.unquote('"hi"')     //=> "hi"

s.lines("hi\nthere")   //=> ["hi", "there"]
s.words("hi  there")  //=> ["hi", "there"]

Padding and Formatting

s.pad("1", 8)               //=> "       1"
s.pad("1", 8, "0")          //=> "00000001"
s.pad("1", 8, " ", "right") //=> "1       "
s.pad("1", 8, " ", "both")  //=> "    1   "

s.numberFormat(1000, 2)     //=> "1,000.00"
s.sprintf("%.1f", 1.17)     //=> "1.2"

References

Related topics