Introduction
In JavaScript, removing whitespaces from a string is a common task, especially when dealing with user input or cleaning up data. Whitespaces can occur at the beginning, end, or within the string. JavaScript provides several ways to remove these spaces, such as using trim()
to remove spaces from the start and end, or using regular expressions to remove all whitespaces from a string.
Problem Statement
Create a JavaScript program that:
- Accepts a string.
- Removes whitespaces from the string either from the beginning and end, or from the entire string.
- Returns and displays the modified string.
Example:
Input:
" Hello World "
Output:
"Hello World"
(removing leading and trailing spaces)Input:
"Hello World"
Output:
"HelloWorld"
(removing all spaces)
Solution Steps
- Read the Input String: Provide the string either via user input or directly in the code.
- Remove Whitespaces:
- Use
trim()
to remove leading and trailing spaces. - Use a regular expression to remove all spaces within the string.
- Use
- Display the Result: Print the modified string.
JavaScript Program
Example 1: Remove Leading and Trailing Whitespaces Using trim()
// JavaScript Program to Remove Leading and Trailing Whitespaces
// Author: https://www.javaguides.net/
function removeLeadingTrailingSpaces(str) {
// Step 1: Use trim() to remove leading and trailing whitespaces
return str.trim();
}
// Example input
let inputString = " Hello World ";
let modifiedString = removeLeadingTrailingSpaces(inputString);
console.log(`Original String: "${inputString}"`);
console.log(`Modified String: "${modifiedString}"`);
Output
Original String: " Hello World "
Modified String: "Hello World"
Example 2: Remove All Whitespaces Using a Regular Expression
// JavaScript Program to Remove All Whitespaces from a String
// Author: https://www.javaguides.net/
function removeAllSpaces(str) {
// Step 1: Use a regular expression to remove all whitespaces
return str.replace(/\s+/g, '');
}
// Example input
let inputString = "Hello World";
let modifiedString = removeAllSpaces(inputString);
console.log(`Original String: "${inputString}"`);
console.log(`Modified String: "${modifiedString}"`);
Output
Original String: "Hello World"
Modified String: "HelloWorld"
Explanation
Step 1: Remove Leading and Trailing Whitespaces Using trim()
- The
trim()
method removes any leading and trailing spaces from a string. This is helpful when you want to clean up input or ensure no extra spaces exist around the content.
Step 2: Remove All Whitespaces Using a Regular Expression
- The regular expression
/\s+/g
is used to match all whitespace characters (\s
) and remove them from the string. Theg
flag ensures that all instances of spaces are removed, including those between words.
Step 3: Return and Display the Result
- The modified string is returned and displayed using
console.log()
.
Conclusion
This JavaScript program demonstrates two ways to remove whitespaces from a string. You can use trim()
to remove spaces from the beginning and end of a string, or you can use regular expressions to remove all spaces within the string. Both methods are useful depending on whether you need to clean up spaces at the boundaries or throughout the entire string.
Comments
Post a Comment
Leave Comment