Using Underscore.string for String Manipulations in JavaScript

Underscore.string is an excellent string manipulation library (or helper) for Javascript that can be used with or without the nifty Underscore.js library. It can be used client side in browsers or server side with Node.js.

Installation

For client side usage you can get the development (uncompressed) or production (compressed) versions. It’s accessible from _.str where _ is a global object.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Just to make things more clear, this is the last portion of the library’s code –

// Integrate with Underscore.js if defined
// or create our own underscore object.
root._ = root._ || {};
root._.string = root._.str = _s;

For Node.js usage, just install via NPM.

$ npm install underscore.string

.. and load in your code.

var _s = require('underscore.string');

If you want to use this library as an extension of Underscore (which also means you’re probably already using underscore), then you’ll have to do something like this –

var _  = require('underscore');

// Loading `underscore.string` to a separate object on
// the `underscore` object, because there are
// conflicting functions in both libraries like `include` etc.
_.str = require('underscore.string');

We just loaded the library on _.str due to conflicting methods. Although it is still possible to mix in all the non-conflicting functions from _.str to _ if you want to.

// Mix in non-conflicting functions to Underscore namespace
_.mixin(_.str.exports());

String Utilities

The extension comes with lots of string utility functions. Let’s take a look at some of them.

capitalize _.capitalize(string)

Converts only the first letter of the string to uppercase.

> _.capitalize("foo bar")
'Foo bar'

classify _.classify(string)

Converts the string to camelized class name.

> _.classify('foo bar')
'FooBar'
> _.classify('foo_bar')
'FooBar'
> _.classify('foo-bar')
'FooBar'
> _.classify('foo$bar')
'FooBar'
> _.classify('foo[bar')
'FooBar'
> _.classify('foo5bar')
'Foo5bar'

underscored _.underscored(string)

Converts a camelized or dasherized string into an underscored one.

> _('fooBar').underscored()
'foo_bar'
> _('FooBar').underscored()
'foo_bar'
> _('foo-bar').underscored()
'foo_bar'
> _('-foo-bar-').underscored()
'_foo_bar_'

trim _.trim(string, [characters])

Trims defined characters from beginning and ending of the string. Defaults to whitespace characters.

> _.trim("  foobar   ")
'foobar'
> _.trim("_-foobar-_", "_-")
'foobar'

include _.include(string, substring)

Check whether substring exists in string or not. Available through _.str object as Underscore has a conflicting function with the same name.

> _.str.include("foobar", "ob")
true

.. and many more!

Few Notes

Apparently for most of the functions both of the following syntaxes work.

> _.classify('foo-bar')
'FooBar'
> _('foo-bar').classify()
'FooBar'

The latter syntax only works when you’re using the module in conjunction with underscore.js. Also you can perfectly chain your method calls –

> _('12foo-bar12').chain().trim('12').classify().value()
'FooBar'

You should use some other variable like u over _ in the interactive Node REPL because _ is used to hold the last return value.

> _ = require('underscore')
...
> 'hello world'
'hello world'
> _
'hello world'

Conclusion

Personally, I found this library to be immensely useful in many projects. So it’s best to get acquainted with it!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *