DEV Community

Cover image for Let's talk about how powerful split function is
Jalal πŸš€
Jalal πŸš€

Posted on

Let's talk about how powerful split function is

Split() function in JavaScript is super cool. It looks into a given string and split it into an array.

const array = "Jan\n Feb\n Mar".split(/\n/)

// ["Jan", " Feb", " Mar"]
Enter fullscreen mode Exit fullscreen mode

So, if we are curious about knowing how many lines in a given string:

const lines = "Jan\n Feb\n Mar".split(/\n/).length

// 3
Enter fullscreen mode Exit fullscreen mode

Let's put our code into one function so we can develop it easily:

/**
 * split string in each line and put it into array.
 */
function splitToLines(lines) {
  const linesArray = lines.split(/\n/);

  return linesArray;
}

const str = "Hello World\n How are you doing";
const strArr = splitToLines(str) // [ 'Hello World', ' How are you doing' ]
const strLength = strArr.length // 2
Enter fullscreen mode Exit fullscreen mode

We may dive more into each line individually by counting words:

/**
 * counts words in an array of lines.
 */
function countWords(linesArr) {
  let words = 0;

  // go into each line individually.
  linesArr.forEach(line => {
    // line1: Hello World
    // line2: How are you doing

    // let's trim the line to avoid spaces in the beginning.
    // split each line by spaces so we can count words in each line.
    const wordsArr = line.trim().split(/\s/);
    // line1-arr: ["Hello", "World"];
    // line2-arr: [ "How", "are", "you", "doing" ]

    words += wordsArr.length;
    // for line1: words equal to 2
    // for line1: words equal to 4
  });
  return words;
}


const str = "Hello World\n How are you doing";
const linesArr = splitToLines(str) //  [ 'Hello World', ' How are you doing' ]
const wordsNum = countWords(linesArr) // 6
const linesNum = linesArr.length // 2
Enter fullscreen mode Exit fullscreen mode

It's fun, exactly like solving a puzzle. You start with the first piece and suddenly you nearly there.

Think about it, if you have lines and words number, you can easily count characters in each word and calculate spaces.

This is exactly how I built a function called textics. It counts lines, words, chars and spaces for a given string using split() function and that all can be done with couple lines of code.


Do you like it? Please leave a ⭐️. I appreciate any feedback or PRs πŸ‘‹πŸ‘‹πŸ‘‹

Top comments (8)

Collapse
 
damnjan_37 profile image
Damnjan Lukovic

Good idea! However, there is an issue with this approach. If you input a string with special characters, like this: "Hello - world", it would count it as 3 words instead of 2. Your code registers anything between two whitespace characters as a word. To solve that problem, we can use regex.

\w is used for matching word characters - it matches a letter, a number or an underscore. To match the whole word, we simply write \w+

So instead of

const wordsArr = line.trim().split(/\s/); 
words += wordsArr.length;

We could write

const wordsArr = line.match(/\w+/g) || []
words += wordsArr.length;

If we want to match alphabetical characters only, we can use [a-zA-Z] instead of \w, or we can play around with the regex based on our needs.

Bonus: you can use map and reduce to make this code shorter:

function countWords(linesArr) {
  return linesArr
    .map(line =>  (line.match(/\w+/g) || []).length)
    .reduce((curr, next) => curr + next)
}

Hope this helps!

Collapse
 
jalal246 profile image
Jalal πŸš€

It helps Damnjan, thanks! I still need to count characters and spaces tho maybe using match(/\s/g) will do with total length.

Collapse
 
pavelloz profile image
PaweΕ‚ Kowalski • Edited

Well, if all you want is to count lines with assumption that \n means new line, i would suggest using something simpler (i assume it would be faster, but thats for someone else to test out).

const text = "hello \n world \n !!!"

const count = (text.match(/\n/g) || []).length + 1;

count // 3
Collapse
 
jalal246 profile image
Jalal πŸš€

Hi PaweΕ‚, thanks for the reply. I am not sure if using match instead of split is simpler or just another way to count lines. And there's another point, using split, enables us to count words, characters, and spaces. How do you achieve this goal with match? I think this adds more complexity instead of developing what we already have.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

I think all these three are similar, . split('\n'), . split(/\n/) and . split(/\n/g).

Do you know that you can . split(/\.(.+)$/)?

Collapse
 
heyrohit profile image
Rohit Gupta

/g in regex is for all instances in a string :)

Collapse
 
tracker1 profile image
Michael J. Ryan

Would suggest that splitLines would use \r?\n as a lot of files on windows will still have crlf line endings.

Collapse
 
jalal246 profile image
Jalal πŸš€

Yes Michael, I totally agree. That's why, originally in gihub repo, I created a getNewLineChar function, to detect new line character for different systems.