Extracting a List from a String
Problem
You have a string with several sentences, one of which includes a list of items. The list
begins with a colon (:) and ends with a period (.), and each item is separated by a comma.
You want to extract just the list.
Before:
This is a list of items: cherries, limes, oranges, apples.
After:
['cherries','limes','oranges','apples']
Solution
The solution requires two actions: extract out the string containing the list of items, and
then convert this string into a list.
Use String’s indexOf() to locate the colon, and then use it again to find the first period
following the colon. With these two locations, extract the string using String’s sub
string():
var sentence = 'This is one sentence. This is a sentence with a list of items:' + 'cherries, oranges, apples, bananas. That was the list of items.'; var start = sentence.indexOf(':'); var end = sentence.indexOf('.', start+1); var listStr = sentence.substring(start+1, end);
Once you have the string consisting of the list items, use the String split() to break
the string into an array:
var fruits = listStr.split(','); console.log(fruits); // ['cherries', ' oranges', ' apples', ' bananas']
EXPLAINED
The index Of () method takes a search value, as first parameter, and an optional begin‐
ning index position, as second.
The list is delimited by a beginning colon character and an ending period. The index
Of() method is used without the second parameter in the first search, to find the colon.
The method is used again but the colon’s position (plus 1) is used to modify the begin‐
ning location of the search for the period:
var end = sentence.indexOf('.',start+1);
If we didn’t modify the search for the ending period, we’d end up with the location of
the first sentence’s period rather than the period for the sentence containing the list.
Once we have the beginning and ending location for the list, we’ll use the sub
string() method, passing in the two index values representing the beginning and end‐
ing positions of the string:
var listStr = sentence.substring(start+1, end);
The extracted string is:
cherries, oranges, apples, bananas
We’ll finish by using split() to split the list into its individual values:
var fruits = listStr.split(',') ; // ['cherries', ' oranges', ' apples', ' bananas']
There is another string extraction method, substr(), that begins extraction at an index
position marking the start of the substring and passing in the length of the substring as
the second parameter. We can easily find the length just by subtracting the beginning
position of the string from the end position:
var listStr = sentence.substr(start+1, end-start); var fruits = listStr.split(',');
Advanced
The result of splitting the extracted string is an array of list items. However, the items
come with artifacts (leading spaces) from sentence white space. In most applications,
we’ll want to clean up the resulting array elements.
We’ll discuss the Array object in more detail in Chapter 2, but for now, we’ll use the
Array for Each() method in addition to the String object’s trim() method to clean up
the array:
fruits = listStr.split(','); console.log(fruits); // [' cherries', ' oranges', ' apples', ' bananas'] fruits.forEach(function(elmnt,indx,arry) { arry[indx] = elmnt.trim(); }); console.log(fruits); // ['cherries', 'oranges', 'apples", "bananas"]
The forEach() method applies the function passed as parameter (the callback) to each
array element. The callback function supports three arguments: the array element value,
and optionally, the array element index and the array itself.
Another simpler approach is to pass a regular expression to the split() that trims the
result before it’s returned:
var fruits = listStr.split(/\s*,\s*/);
Now the matching returned value is just the string without the surrounding white space.
Simplifying the Code Using Chaining
The example code in this recipe is correct, but a little verbose. We can compress the
code by using JavaScript’s method chaining, allowing us to attach one method call to the
end of a previous method call if the object and methods allow it. In this case, we can
chain the split() method directly to the substring() method:
var start = sentence.indexOf(":"); var end = sentence.indexOf(".", start+1); var fruits = sentence.substring(start+1, end).split(",");
No comments:
Post a Comment