We will replace all dots in a string using javascript. There are 4 methods to replace the dots(.) in the string all of them are described with examples.
Methods to Replace All Dots in a String:
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.
Syntax:
str.replace(A, B)
Example: we are replacing the dots(.) with space( ) in the text “A.Computer.science.Portal”.
javascript
let str = "A.Computer.science.Portal";
let res = str.replace(/\./g, " ");
console.log("String 1: " + str);
console.log("String 2: " + res);
|
Output
String 1: A.Computer.science.Portal
String 2: A Computer science Portal
We can split up strings of text with the Javascript split method and join strings using the replace characters with the join method.
Syntax:
string.split('.').join(' ');
Example: we are replacing the dots(.) with space( ) using split and join.
javascript
let str = "A.Computer.Science.portal";
let newStr = str.split(".").join(" ");
console.log("String 1: " + str);
console.log("String 2: " + newStr);
|
Output
String 1: A.Computer.Science.portal
String 2: A Computer Science portal
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.
Syntax:
[...str].reduce( (accum, char) => (char==='.') ? accum : accum + char , '')
Example: In this example, we will replace ( ‘.’ ) by using the spread operator and reduce function.
Javascript
let str = "A.Computer.Science.portal";
let newStr = [...str].reduce(
(s, c) => (c === "." ? s : s + c),""
);
console.log("String 1: " + str);
console.log("String 2: " + newStr);
|
Output
String 1: A.Computer.Science.portal
String 2: AComputerScienceportal
The 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.
Syntax:
const newString = originalString.replaceAll(regexp | substr , newSubstr | function)
Example: This example uses the replaceAll() method to replace ( ‘.’ ).
Javascript
let str = "Geeks.for.Geeks";
let replacedStr = str.replaceAll(".", "");
console.log("Original string : " + str);
console.log("Modified string : " + replacedStr);
|
Output
Original string : Geeks.for.Geeks
Modified string : GeeksforGeeks
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
21 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...