Referință rapidă

Ghid Rapid Underscore.string

Ghid rapid pentru biblioteca underscore.string în JavaScript: conversia literelor, aliniere, curățare și formatarea numerelor.

· Publicat

underscore.string oferă extensii pentru manipularea șirurilor de caractere în JavaScript.

Conversie majuscule/minuscule și formate

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"

Inspectarea șirurilor

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

Entități HTML și eliminarea etichetelor

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

Ghilimele și divizarea șirurilor

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

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

Completare (Padding) și formatare

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"

Referințe

Subiecte conexe