Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript program to remove vowels from a string
A string is a sequence of characters that can include alphabets, numbers, and symbols together or separately. In this article, we are going to learn how we can remove vowels from a given string in JavaScript using different approaches.
The five vowels in the English alphabet are: a, e, i, o, u, and they can be both uppercase or lowercase.
Problem Examples
Example 1
Input: Anshu Ayush Output: nsh ysh
Explanation
The vowels in the string "Anshu Ayush" are A, u, A, u. After removing them, the remaining string is "nsh ysh".
Example 2
Input: crypt Output: crypt
Explanation
The string "crypt" contains no vowels, so the output remains the same.
Example 3
Input: Tutorials point Output: Ttrls pnt
Explanation
The vowels in the string are u, o, i, a, o, i. After removing them, the final string is "Ttrls pnt".
Using Direct Logic Approach
In this approach, we use a simple loop to traverse the string completely and check if each character is a vowel or not. If it is a vowel then we skip the character while constructing the new string.
Steps for Implementation
- First, we take a string as the input.
- Now, initialize an empty string to store the result.
- Loop through each character in the string.
- For each character, check if it is a vowel or not.
- If it is not a vowel, add it to the result string.
- Return the modified string.
Implementation Code
let inputStr = "Anshu Ayush";
let outputStr = "";
// Convert string to lowercase for comparison
for (let i = 0; i < inputStr.length; i++) {
if (!["a", "e", "i", "o", "u"].includes(inputStr[i].toLowerCase())) {
outputStr += inputStr[i];
}
}
// Output
console.log("The string after removing vowels is:", outputStr);
The string after removing vowels is: nsh ysh
Time Complexity: O(n)
Space Complexity: O(n)
Using Function Approach
In this approach, we use a function to remove the vowels from a string. This allows us to reuse the logic whenever needed again.
Implementation Code
// Function to remove vowels
function removeVowelsFromString(str) {
let resultStr = "";
for (let i = 0; i < str.length; i++) {
if (!["a", "e", "i", "o", "u"].includes(str[i].toLowerCase())) {
resultStr += str[i];
}
}
return resultStr;
}
let inputString = "Tutorials point";
let modifiedOutput = removeVowelsFromString(inputString);
console.log("Original string:", inputString);
console.log("After removing vowels:", modifiedOutput);
Original string: Tutorials point After removing vowels: Ttrls pnt
Using Regular Expression
Regular expressions provide a more concise way to remove vowels using the replace() method with a pattern.
function removeVowelsWithRegex(str) {
return str.replace(/[aeiouAEIOU]/g, "");
}
let testString = "JavaScript Programming";
let result = removeVowelsWithRegex(testString);
console.log("Original string:", testString);
console.log("After removing vowels:", result);
Original string: JavaScript Programming After removing vowels: JvScrpt Prgrmmng
Using Filter Method
We can also use the filter() method with array operations to remove vowels.
function removeVowelsWithFilter(str) {
const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
return str.split('').filter(char => !vowels.includes(char)).join('');
}
let sampleText = "Hello World";
let filteredText = removeVowelsWithFilter(sampleText);
console.log("Original string:", sampleText);
console.log("After removing vowels:", filteredText);
Original string: Hello World After removing vowels: Hll Wrld
Performance Comparison
| Method | Time Complexity | Readability | Performance |
|---|---|---|---|
| Direct Loop | O(n) | Good | Fast |
| Function | O(n) | Excellent | Fast |
| Regular Expression | O(n) | Very Good | Good |
| Filter Method | O(n) | Good | Moderate |
Conclusion
All methods effectively remove vowels from strings. The function approach offers the best reusability, while regular expressions provide the most concise code. Choose based on your specific requirements and coding style preferences.
