-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathRemoveStringSpaces.java
More file actions
31 lines (20 loc) · 768 Bytes
/
RemoveStringSpaces.java
File metadata and controls
31 lines (20 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.zetcode;
import java.util.StringJoiner;
public class RemoveStringSpaces {
public static void main(String[] args) {
String msg = """
Today is a beautiful day.
There are no clouds in the sky.
""";
String[] parts = msg.split("[\\s]{2,}");
var joiner = new StringJoiner(" ");
for (var cs: parts) {
joiner.add(cs);
}
String modified = joiner.toString();
System.out.println(modified);
// String modified = msg.replaceAll("[\\s]{2,}", " ");
// System.out.println(modified);
// System.out.println(msg);
}
}