Warning: session_start(): open(/tmp/sess_0a299dbc91ed49c51246484c79da84c6, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Split A String Keeping The Whitespace Using JavaScript - LearnShareIT

How To Split A String Keeping The Whitespace Using JavaScript

Split a String keeping the Whitespace using JavaScript

Spit a string is a common problem in JS programming. We have one task to do which is split a String keeping the Whitespace using JavaScript? Let’s check the post in below to see the answer.

Split a String keeping the Whitespace using JavaScript – How to do it?

Here are three ways to do that. Read each method carefully to find out what works for you.

Using the split() and push() function.

The split() function works directly on the string and splits the string into an array of substrings.

split() syntax:

string.split(divider, limit)

Parameters:

  • divider: the character used to separate the string
  • limit: limit the number of splits

push() syntax:

array.push(item1, item2, ...)

Parameters:

  • array: the array that you need to push the item to
  • item1, item2: items to push into the array

push() is used to add items to the array, and push() must have at least one argument.

The idea is that we will use split() to separate the elements by a Whitespace. Then we push them and push the Whitespace respectively into an empty array. Remember that not to push a Whitespace in the last loop. Like this:

const string = 'learn share IT';
const stringArr = string.split(' ');
const result = [];

stringArr.forEach((value, index) => {

  // Don't push the last whitespace
  if (index === stringArr.length - 1) {
    result.push(value);
    return;
  }

  // Push value then push whitespace
  result.push(value);
  result.push(' ');
});

console.log(result);

Output:

(5) ['learn', ' ', 'share', ' ', 'IT']

Using split() and join() function.

join() Syntax:

array.join(separator)

Description:

The join() function returns a string concatenated by the elements in the array, the elements in the string will be separated by the separator.

First, we use the split() function to split the string and return an array, and then we use the join() function to concatenate those strings with any character you want, but there must be a Whitespace in the middle, the start and end characters must be the same. Finally, we use the split() function to split the concatenated string with the separator, which is the one character used in the join() function. It doesn’t sound very clear, look at the example below, and you will see that it is actually easy to understand.

const string = 'learn share IT';

// Split with whitespace
let stringTemp = string.split(' ');

// Join with the 2 same characters and space in between
stringTemp = stringTemp.join('@ @');

// Split with the specified character
const result = stringTemp.split('@');

console.log(result);

Output:

(5) ['learn', ' ', 'share', ' ', 'IT']

Using split() and replace() function

replace() syntax:

replace(searchStr, replaceStr)

Parameters:

  • searchStr: the pattern you want to search and replace. This pattern can be a regex.
  • replaceStr: string to replace searchStr.

The replace() function returns a new string with the searchStr replaced by replaceStr. So we will use replace() to replace all Whitespace with the same two characters. Then, we use split() to split the new string with the character we used. Now the return result contains empty elements. We just need to loop through and replace the empty element with a space. Like this:

const string = 'learn share IT';

// Replace all ' ' by '--'
let stringTemp = string.replace(/ /g, '--'); // learn--share--IT

// Split with '-'
let result = stringTemp.split('-'); // ['learn', '', 'share', '', 'IT']

// Replace empty string by 1 whitespace
for (i in result) {
  if (result[i] === '') {
    result[i] = ' ';
  }
}

console.log(result);

Output:

(5) ['learn', ' ', 'share', ' ', 'IT']

Summary

Above are three simple ways to split a String and keep the Whitespace in JS. If any of the three ways above still confuse you, please comment below!

Have a nice day!

Maybe you are interested:

Leave a Reply

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