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"]
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
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
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
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)
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
We could write
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
andreduce
to make this code shorter:Hope this helps!
It helps Damnjan, thanks! I still need to count characters and spaces tho maybe using
match(/\s/g)
will do with total length.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).Hi PaweΕ, thanks for the reply. I am not sure if using
match
instead ofsplit
is simpler or just another way to count lines. And there's another point, usingsplit
, enables us to count words, characters, and spaces. How do you achieve this goal withmatch
? I think this adds more complexity instead of developing what we already have.I think all these three are similar,
. split('\n')
,. split(/\n/)
and. split(/\n/g)
.Do you know that you can
. split(/\.(.+)$/)
?/g in regex is for all instances in a string :)
Would suggest that splitLines would use
\r?\n
as a lot of files on windows will still have crlf line endings.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.