How to use Split() and Join() Method In Javascript

We can split up strings of text with the JavaScript split() method and join() method to join strings using the replace characters with the join method. 

Syntax:

string.split('.').join(' ');

Example: Here we are replacing the dots(.) with space( ) using split and join. 

javascript
// Assigning a string
let str = "A.Computer.Science.portal";

// Calling split(), join() function
let newStr = str.split(".").join(" ");

// Printing original string
console.log("String 1: " + str);

// Printing replaced string
console.log("String 2: " + newStr);

Output
String 1: A.Computer.Science.portal
String 2: A Computer Science portal

How to replace all dots in a string using JavaScript ?

We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript.

Table of Content

  • Using JavaScript replace() Method
  • Using JavaScript Split() and Join() Method
  • Using JavaSccript reduce() Method and spread operator
  • Using JavaScript replaceAll() Method
  • Using JavaScript for loop
  • Using JavaScript map() Method on Arrays

Similar Reads

Using JavaScript replace() Method

The string.replace() function is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged....

Using JavaScript Split() and Join() Method

We can split up strings of text with the JavaScript split() method and join() method to join strings using the replace characters with the join method....

Using JavaSccript reduce() Method and spread operator

We can use the spread operator to make an array from the character of a string and form a string with the help of reduce() method without dots in the string....

Using JavaScript replaceAll() Method

JavaScript replaceAll() method returns a new string after replacing all the matches of a string with a specified string or a regular expression. The original string is left unchanged after this operation....

Using JavaScript for loop

We iterates over each character in the string using a for loop. If the current character is a dot (‘.’), it appends the newChar character to the result; otherwise, it appends the current character from the input string. Finally, it returns the result string, which contains the replaced characters....

Using JavaScript map() Method on Arrays

We can convert the string into an array of characters, use the map() method to iterate over each character, and replace dots with spaces. Finally, we join the array back into a string.Example: In this example, we replace dots (‘.’) with spaces (‘ ‘) using the map() method....

Contact Us