In this section I will be starting out with a few quick examples of using lodash to work with strings. This will not be an overview of the various lodash string methods, or how to go about doing similar things without the use of lodash, as these are things that I will intend to get to in later sections in this post.
Maybe a good hello would type of method to start out with would be the split method, if I pass a string as the first argument an then a string to split by as the second the result will be an array where each element is a sub string between each instance of the string that was given to split the source string with.
So then the lodash split is an example of a method that will take a string as a first argument and then the method returns an array. Although the return value is an array the source that that method works with is very much a string, so then it is a kind of string method in lodash. However often one will need to also work with other lodash methods that are array methods, collection methods, or object methods in order to complete some kind of over all greater task. As you can see the end result of this example is an array with some empty strings as elements, as such I will typically want those elements removed, and then do something more with the array after that.
So now that I have a very basic hello world style example out of the way when it comes to string methods in lodash the next step might be something a little more advanced. When it comes to chaining methods in lodash there are a few options to choose from, one of which is the lodash chain method. When calling the chain method the first method that I can pass to it could be a string value, but then when it comes to calling another method off of chain it will then need to be a string method, or at least some method that will work with a string value. As with the basic example above this can be the split method again for examples as that is of course a string method in lodash.
After the split method is called then I can use some kind of method in lodash that will work with an array to remove those empty string values that I do not want. There are a few options for this, one of which is the compact method that works well for this examples at least. I can then use the clean array of the empty string removed now with another method such as the lodash map collection method to create an array of values based off of the values from the source string. There is then array methods that can be used to create a string primitive value from an array of values, one such options with this would be the join method.
For another example of basic lodash string methid examples another good one to cover right away would be the lodash words method.
Now that I have some ery basics of string methods in lodash, and how to use them with other lodash methods, in this section I will be going over some of the various string methods in lodash. I will not be going over every one of them here of course, if you really want to know what each of them are you can always go to the lodash documentation website first and foremost. However I think I should at least briefly go over a few of them to say the least before moving on to something else in this post.
The lodash deburr method is a method that can be used to convert the characters of text from various languages to an equivalent character that is in the ASCII range.
The escape method can be used to escape text so that it will render as plain text rather than html. However it is just that kine of escape for text, when it comes to urls, or anything else to that effect this method will fall short.
The lodash pad method, as well as several other similar methods can be used to create a new string that contains additional padding to the left or right of the string, up to a set size.
As with the lodash split method there is also a native split method in the string prototype.
The replace string method can be used to replace one or more instances of a text pattern with another.