Get The First N Words From A String In JavaScript

Get the first N words from a String in JavaScript

To get the first N words from a string in JavaScript you can use the split() and slice() methods. You must know these skills if you are working with strings in Javascript.

Get the first N words from a string in JavaScript

Using split() and slice()

The slice() method will return the first N elements in an array starting from a specific index given.

Syntax:

split(separator)

Parameter:

  • separator: The regex or a character to separate between words in a string.

slice(start, count)

Parameter:

  • start: The starting point (index) of the first words (elements).
  • count: The number of how many words you want to remove.

You can use the slice() function to get some elements starting from 0 index, which means removing the first N elements from an array.

let string =  'learn share it helps you with tutorials'

// Split the string into an array of words 
let array = string.split(' ')

// Get the first 3 words from a string
array = array.slice(0,3)

console.log(array)

// Convert the array into a sentence
console.log(array.join(' '))

Output: 

['learn', 'share', 'it']
learn share it

The above example shows that we have an array containing 3 words (as in the output) after using the split() and slice(0,3) methods. In some cases, you may want to get the words sequentially as in a sentence; hence we have converted that array of 3 words back to a string using the join() method. However, this is not required because sometimes you may need an array of words rather than a concatenated string.

Using _.join() and_.split()

These two methods require the library lodash. To import this library, please declare this in your document:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Syntax:

_.split(str, sep, len)

Parameters:

  • str: the string you want the get first N words.
  • sep: the separator in which distinguish between words in a string.
  • len: the total number of words you want to split.

The lodash split() also works on a regex expression when your separators has a repetitive pattern such as one or many whitespaces.

let string =  'learn  share   it helps you with tutorials'

// Split the string into an array of first words 
let array = _.split(string, / +/, 3)

console.log(array)

Output:

['learn', 'share', 'it']

If you want to get a sentence containing the first N words from a string, you can use the _.join() method.

Syntax:

_.join(arr, sep)

Parameters:

  • arr: the array you want the convert into string.
  • sep: the separator in which distinguishes between words in a string, by default it is equals to ‘,’ if not declared.
let string =  'learn  share   it helps you with tutorials'

// Split the string into an array of 3 first words 
let array = _.split(string, / +/, 3)

console.log(array)

// Convert the array into a string of first 3 words with whitespace as separator
let sentence = _.join(array,' ')

console.log(sentence)

Output: 

['learn', 'share', 'it']
learn share it

Although this solution need to import an external library, it produces the same results as the first one.

Summary

We have learned to get the first N words from a string in JavaScript in two different approaches. With the use of our guide in this tutorial, you can complete the task successfully. Good luck for you!

Maybe you are interested:

Leave a Reply

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