Skip to content

Commit 48b1551

Browse files
committed
feat: Normalize whitespace by removing extra spaces from a string
- Input: String containing irregular and excessive whitespace between words. Example: " abc de fgh ijk" - Used `str.replaceAll("\\s+", " ")` to: - Match and replace one or more whitespace characters (`\\s+`) with a single space. - Applied `.trim()` to remove leading and trailing spaces from the result. 🔍 Explanation: - `\\s` matches any whitespace character (space, tab, newline). - `\\s+` matches a sequence of one or more whitespace characters. - `trim()` ensures no leftover spaces at the beginning or end. 🧪 Output: - Before: `" abc de fgh ijk"` - After: `"abc de fgh ijk"` ✅ Useful for cleaning and formatting user input, logs, or data for storage/display. Signed-off-by: Somesh diwan <[email protected]>
1 parent a06e491 commit 48b1551

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
/*
2-
Remove extra spaces from string
3-
Logic: String str = "abc de fgh ijk"
4-
Method used here: str.ReplaceAll("\\s", " ");
5-
Small s means space & Capital S means not a space.
6-
*/
7-
81
public class RegularExpressionChallenge4 {
92
public static void main(String[] args) {
103
String str = " abc de fgh ijk";
114
System.out.println(str.replaceAll("\\s+", " ").trim());
125
}
13-
}
6+
}
7+
8+
/*
9+
Remove extra spaces from string.
10+
Logic: String str = "abc de fgh ijk"
11+
12+
Method used here: str.ReplaceAll("\\s", " ");
13+
Small s means space & Capital S means not a space.
14+
*/

0 commit comments

Comments
 (0)