{"id":1065,"date":"2024-03-11T18:20:37","date_gmt":"2024-03-11T12:50:37","guid":{"rendered":"http:\/\/codetheory.in\/?p=1065---88eea1ae-2e92-4a78-9d6c-534624d9c875"},"modified":"2024-03-11T18:20:37","modified_gmt":"2024-03-11T12:50:37","slug":"using-underscore-string-for-string-manipulations-in-javascript","status":"publish","type":"post","link":"https:\/\/codetheory.in\/using-underscore-string-for-string-manipulations-in-javascript\/","title":{"rendered":"Using Underscore.string for String Manipulations in JavaScript"},"content":{"rendered":"
Underscore.string<\/a> is an excellent string manipulation library (or helper) for Javascript that can be used with or without the nifty Underscore.js<\/a> library. It can be used client side in browsers or server side with Node.js.<\/p>\n <\/p>\n For client side usage you can get the development<\/a> (uncompressed) or production<\/a> (compressed) versions. It’s accessible from Just to make things more clear, this is the last portion of the library’s code –<\/p>\n For Node.js usage, just install via NPM.<\/p>\n .. and load in your code.<\/p>\n 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 –<\/p>\n We just loaded the library on The extension comes with lots of string utility functions. Let’s take a look at some of them.<\/p>\n capitalize<\/strong> Converts only the first letter of the string to uppercase.<\/p>\n classify<\/strong> Converts the string to camelized class name.<\/p>\n underscored<\/strong> _.underscored(string)<\/p>\n Converts a camelized or dasherized string into an underscored one.<\/p>\n trim<\/strong> _.trim(string, [characters])<\/p>\n Trims defined characters from beginning and ending of the string. Defaults to whitespace characters.<\/p>\nInstallation<\/h3>\n
_.str<\/code> where
_<\/code> is a global object.<\/p>\n
\r\n\/\/ Integrate with Underscore.js if defined\r\n\/\/ or create our own underscore object.\r\nroot._ = root._ || {};\r\nroot._.string = root._.str = _s;\r\n<\/pre>\n
\r\n$ npm install underscore.string\r\n<\/pre>\n
\r\nvar _s = require('underscore.string');\r\n<\/pre>\n
\r\nvar _ = require('underscore');\r\n\r\n\/\/ Loading `underscore.string` to a separate object on\r\n\/\/ the `underscore` object, because there are\r\n\/\/ conflicting functions in both libraries like `include` etc.\r\n_.str = require('underscore.string');\r\n<\/pre>\n
_.str<\/code> due to conflicting methods. Although it is still possible to mix in all the non-conflicting functions from
_.str<\/code> to
_<\/code> if you want to.<\/p>\n
\r\n\/\/ Mix in non-conflicting functions to Underscore namespace\r\n_.mixin(_.str.exports());\r\n<\/pre>\n
String Utilities<\/h3>\n
_.capitalize(string)<\/code><\/p>\n
\r\n> _.capitalize("foo bar")\r\n'Foo bar'\r\n<\/pre>\n
_.classify(string)<\/code><\/p>\n
\r\n> _.classify('foo bar')\r\n'FooBar'\r\n> _.classify('foo_bar')\r\n'FooBar'\r\n> _.classify('foo-bar')\r\n'FooBar'\r\n> _.classify('foo$bar')\r\n'FooBar'\r\n> _.classify('foo[bar')\r\n'FooBar'\r\n> _.classify('foo5bar')\r\n'Foo5bar'\r\n<\/pre>\n
\r\n> _('fooBar').underscored()\r\n'foo_bar'\r\n> _('FooBar').underscored()\r\n'foo_bar'\r\n> _('foo-bar').underscored()\r\n'foo_bar'\r\n> _('-foo-bar-').underscored()\r\n'_foo_bar_'\r\n<\/pre>\n
\r\n> _.trim(" foobar ")\r\n'foobar'\r\n> _.trim("_-foobar-_", "_-")\r\n'foobar'\r\n<\/pre>\n