Commit 48b1551
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- Section6StringClassPrinting/src
1 file changed
+9
-8
lines changedLines changed: 9 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | 1 | | |
9 | 2 | | |
10 | 3 | | |
11 | 4 | | |
12 | 5 | | |
13 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
0 commit comments