How to Remove Punctuation from String using TypeScript ?
Last Updated :
14 May, 2024
Typescript allows us to remove the punctuation from the string using various methods. Punctuation refers to symbols like commas, periods, and exclamation marks. In TypeScript, to remove punctuation from a string by using these approaches :
Using regular expression
In this approach, we will be using the regular expression to find the punctuation using a pattern and using the replace() method to replace the punctuation with blank spaces to remove them from a string.
Example: This example uses regular expressions to remove the punctuation from a string.
JavaScript
function removePunctuation(text: string): string {
return text
.replace(/[^\w\s]|_/g, "");
}
const text = "Hello, Geek! How are you?";
const cleanText = removePunctuation(text);
console.log(cleanText);
Output:
Hello Geek How are you
Using for loop
In this approach we iterate over each character in the input string using a for loop. For each character, we check if it is a punctuation character by comparing it against a predefined array of punctuation characters. If the character is not a punctuation, we append it to a new string which will eventually contain the cleaned text without any punctuation.
Example: This example uses for loop to remove the punctuation from string.
JavaScript
function removePunctuation(text: string): string {
const punctuation = [".", ",", "!", "?", ";", ":"];
let cleanText = "";
for (let i = 0; i < text.length; i++) {
const char = text[i];
if (!punctuation.includes(char)) {
cleanText += char;
}
}
return cleanText;
}
const text = "Hello user, Welcome to GeeksForGeeks!";
const cleanText = removePunctuation(text);
console.log(cleanText);
Output:
Hello user Welcome to GeeksForGeeks
Using split, filter, and join
In this approach, we use split, filter, and join in JavaScript to remove punctuation from a string. We start by splitting the string into an array of characters, then filter out characters that are not letters, numbers, or spaces and finally join the filtered array back into a string.
Example: This example uses split, filter, and join methods to remove the punctuation from string.
JavaScript
function removePunctuation(str: string): string {
return str.split('').filter(char => /[a-zA-Z0-9 ]/.test(char)).join('');
}
let text: string = "Hello user, Welcome to GFG!";
let cleanedText: string = removePunctuation(text);
console.log(cleanedText);
Output:
Hello user Welcome to GFG
Using ASCII Values
This approach iterates over each character in the input string. For each character, it checks if the character code falls within the range of uppercase letters, lowercase letters, or is equal to the character code for space. If the character is within one of these ranges, it is considered not to be punctuation and is added to the result string.
Example: This example uses ASCII Values to remove the punctuation from string.
JavaScript
function removePunctuation(str: string): string {
let result = '';
for (let i = 0; i < str.length; i++) {
const charCode = str.charCodeAt(i);
if ((charCode >= 65 && charCode <= 90) || // Uppercase letters
(charCode >= 97 && charCode <= 122) || // Lowercase letters
(charCode >= 48 && charCode <= 57) || // Digits
charCode === 32) { // Space character
result += str[i];
}
}
return result;
}
const inputString = "Hello Geek, Welcome to GFG!";
const resultString = removePunctuation(inputString);
console.log(resultString);
Output:
Hello Geek Welcome to GFG
Using String.prototype.replace() with Predefined Punctuation Characters
In this approach, we define a set of punctuation characters and use the String.prototype.replace() method to remove them from the string. We iterate over each punctuation character and replace it with an empty string, effectively removing all punctuation from the string.
Example: This example demonstrates how to remove punctuation from a string using String.prototype.replace() with a predefined set of punctuation characters.
JavaScript
function removePunctuation(text: string): string {
const punctuation = [".", ",", "!", "?", ";", ":"];
punctuation.forEach(p => {
text = text.replace(new RegExp("\\" + p, "g"), "");
});
return text;
}
const text = "Hello, world! How are you?";
const cleanText = removePunctuation(text);
console.log(cleanText);
Output:
"Hello Geek How are you"
Share your thoughts in the comments
Please Login to comment...