Horje
Add Characters to a String in JavaScript

In JavaScript, A string is a combination of multiple characters joined together to form a meaningful word. It can be a name or anything else. You can add an extra character to a string after it is defined to complete it or to practice adding characters.

There are several ways available in JavaScript to achieve this task as listed below:

Using ‘+’ operator

The + operator can be used to concat or add an extra character to the string by using it in between the string and the character to be added.

NOTE: It allows you to add characters either at the start or at the end of a string.

Syntax:

"initialString" + "extraCharacter";

Example: The below code implements the + operator to add a character to a string in JavaScript.

JavaScript
const str1 = "GeeksforGeek" + "s";
const str2 = "J" + "avaScript";
console.log(str1, str2);

Output
GeeksforGeeks JavaScript

Using the concat() method

The string concat() method in JavaScript is used to merge two or more strings and returns a new string that is the result of a combination of the specified string. We can also use it to add a character to the string.

NOTE: It allows you to add characters only at the end of the string.

Syntax:

initialString.concat('extraCharacter');

Example: The below code uses the concat() method to add character to a string in JavaScript.

JavaScript
const str1 = "GeeksforGeek";
const combStr1 = str1.concat('s');
const str2 = "JavaScrip";
const combStr2 = str2.concat('t');
console.log(combStr1, combStr2);

Output
GeeksforGeeks JavaScript

Using template literals

The template literals can also be used to add character to the string by storing the character into a variable and insert it at any position in the string using the template literal syntax.

NOTE: It allows you to add character at any position in the string.

Syntax:

`initialString${extraCharacter}`;

Example: The below code illustrates the use of the template literals to add a character to the string in JavaScript.

JavaScript
const extraChar1 = 'f';
const str1 = `Geeks${extraChar1}orGeeks`;
const extraChar2 = 'S';
const str2 = `Java${extraChar2}cript`;
console.log(str1, str2);

Output
GeeksforGeeks JavaScript

Using slice() method

The slice() method can be used to add a character to a string by removing the string after the index where you want to add the character and then again use the slice() method with the original string to get back the removed string.

Syntax:

initialStr.slice(startInd, endInd) + 'extraCharacter' + initialStr.slice(indexAfterWhichTheStringWasRemoved);

Example: The below code is a practical implementation of the slice() method to add charater to a string.

JavaScript
const str1 = "GeeksfoGeeks"
const updatedStr1 = str1.slice(0, 7) + 'r' + str1.slice(7);
const str2 = "JavaScrpt"
const updatedStr2 = str2.slice(0, 7) + 'i' + str2.slice(7);
console.log(updatedStr1, updatedStr2);

Output
GeeksforGeeks JavaScript

Using substring() method

The substring() method of the string can also be used to add a character to a string in the same way we use the slice() method by removing and adding the string to add the character at specified position.

Syntax:

initialStr.substring(startInd, endInd) + 'extraCharacter' + 
initialStr.substring(indexAfterWhichTheStringWasRemoved);

Example: The below code implements the substring() method to add a character to the string.

JavaScript
const str1 = "GeekforGeeks"
const updatedStr1 = str1.substring(0, 4) + 's' + str1.substring(4);
const str2 = "JaaScript"
const updatedStr2 = str2.substring(0, 2) + 'v' + str2.substring(2);
console.log(updatedStr1, updatedStr2);

Output
GeeksforGeeks JavaScript

Using substr() method

The substr() method can be used to split a string into a small sub string then add the character and after that get the removed substring from the original string to completer it.

Syntax:

initialStr.substr(startInd, endInd) + 'extraCharacter' + 
initialStr.substr(indexAfterWhichTheStringWasRemoved);

Example: The below code explains the use of the substr() method to add a character to the string in JavaScript.

JavaScript
const str1 = "GeesforGeeks"
const updatedStr1 = str1.substr(0, 3) + 'k' + str1.substr(3);
const str2 = "JavaSript"
const updatedStr2 = str2.substr(0, 5) + 'c' + str2.substr(5);
console.log(updatedStr1, updatedStr2);

Output
GeeksforGeeks JavaScript

Using Array.prototype.join() Method

The Array.prototype.join() method can be utilized to add a character to a string. By converting the string to an array of characters, inserting the new character at the desired position, and then joining the array back into a string, you can achieve this task.

NOTE: This method allows you to add characters at any position in the string.

Example: The below code demonstrates the use of the Array.prototype.join() method to add a character to a string in JavaScript.

JavaScript
let str = "hello world";
let charToAdd = "-";
let positionToAdd = 5;

// Convert the string to an array of characters
let charArray = str.split("");

// Insert the new character at the desired position
charArray.splice(positionToAdd, 0, charToAdd);

// Join the array back into a string
let modifiedStr = charArray.join("");

console.log(modifiedStr); // Output: "hello-world world"

Output
hello- world

Using Array.prototype.splice() Method

In this approach, we utilize the Array.prototype.splice() method to add a character to a string at a specified position. This method allows us to modify the contents of an array by removing, replacing, or adding elements. By converting the string into an array, we can insert a new character at any desired position and then join the array back into a string.

Steps:

  1. Convert the string into an array of characters using split('').
  2. Use the splice() method to add the new character at the desired position.
  3. Join the array back into a string using join('').

Example:

This example demonstrates how to add a character to a string using the splice() method.

JavaScript
// Function to add a character to a string using splice()
function addCharacterUsingSplice(str, charToAdd, position) {
    // Convert the string to an array of characters
    let charArray = str.split('');
    
    // Insert the new character at the desired position
    charArray.splice(position, 0, charToAdd);
    
    // Join the array back into a string
    return charArray.join('');
}


const str1 = "HelloWorld";
const updatedStr1 = addCharacterUsingSplice(str1, ' ', 5);
const str2 = "JavaScript";
const updatedStr2 = addCharacterUsingSplice(str2, '-', 4);

console.log(updatedStr1); // Output: "Hello World"
console.log(updatedStr2); // Output: "Java-Script"

Output
Hello World
Java-Script

Using padEnd() Methods

JavaScript provides padEnd() and padStart() methods to pad the current string with another string until the resulting string reaches the given length. These methods can also be used to add characters to the beginning or the end of a string.

Example: The padEnd() method pads the current string with a given string (repeated, if needed) until the resulting string reaches the specified length.

JavaScript
function addCharacterUsingPadEnd(str, char, length) {
    return str.padEnd(str.length + length, char);
}


const originalString1 = "Hello";
const characterToAdd1 = "!";
console.log(addCharacterUsingPadEnd(originalString1, characterToAdd1, 1));

Output
Hello!



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Split a String into an Array of Words in JavaScript Split a String into an Array of Words in JavaScript
What is a Set in JavaScript? What is a Set in JavaScript?
Is set Ordered in JavaScript ? Is set Ordered in JavaScript ?
What is () => in JavaScript ? What is () => in JavaScript ?
What is the pop() method used for in JavaScript Arrays ? What is the pop() method used for in JavaScript Arrays ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12