How To Insert A Character After Every N Characters In JavaScript

Insert a Character after every N Characters in JavaScript

Javascript has built a lot of built-in methods to help us do something. We’ve researched and found the proper built-in methods to insert a character after every N characters in JavaScript. Follow the tutorial below to learn how to do it.

Insert a character after every N characters in JavaScript

After researching, we found the proper methods and combined them to insert a character after every N character in Javascript. Two ways that we guide here are straightforward to understand and detailed.

Use substr() and join() methods

See how we put these methods together. Here we not only show you how to do it but also our way of thinking.

substr() method

This method takes part of a string without affecting the parent string.

Syntax:

string.substr(start, length)

Parameters:

  •   start: Start position to get a string part.
  •   end: The number of characters you want to get.

join() method

This method is used to concatenate the elements of an array into a string, separated by a user-entered character. The default character will be commas.

Syntax:

array.join(separator)

Parameter:

  • separator: is the character that separates elements from each other; default is comma ‘,’

Example:

function splitString(inputStr, n) {
	var result = [];
	for (i = 0; i < inputStr.length; i += n) {
		result.push(inputStr.substr(i, n));
	}
	return result;
}

var arrSplitString = splitString("learnShareIT", 5);
var result = arrSplitString.join("@");

console.log("Split string: ", arrSplitString);
console.log("Result: ", result);

Output:

Split string: (3) ['learn', 'Share', 'IT']
Result: learn@Share@IT

We will now detail why we do this. In this example, we will insert one character every 5 (n) characters. First, we use a loop to get 5 characters one after another until the end of the string using the substr() method and add it to the “results” array. After taking 5 characters in turn until the end of the string is the “result” array element, we combine with the join() method to join the array along with the “@” character (the character you want to insert) into a string.

Use replace() method

replace() method

As a replacement string method, it will return a new string and not affect the original string.

Syntax:

string.replace(searchValue, newValue)

Parameters:

  • searchValue: A string or regular expression to search for.
  • newValue: Is the new value to replace ‘searchValue’.

Example:

var inputStr = "learnShareIT";

// Insert @ character after every 5 character
var result = inputStr.replace(/.{5}/g, "$&@");

console.log("Result: ", result);

Output:

Result:  learn@Share@IT

Now we will explain the regular expression we use for your understanding:

  •   ‘.’: any character except line break
  •   {5}: matches the previous 5 codes
  •   ‘g’: find all matches that match the pattern

The second parameter: is a replacement string, but the special thing here is the use of a special pattern. “$&” is the substring we matched. “$&@” is a replacement string consisting of the matched substring and adding the “@” character.

Summary

This tutorial shows you two simple ways to insert a character after every N characters in JavaScript in detail, easy to understand and fast. There are many tutorials like this; it is beneficial for you. If you want to see more tutorials like this, follow us, we have lots of detailed and easy-to-understand tutorials for you.

Maybe you are interested:

Leave a Reply

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