-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReformatString.java
More file actions
41 lines (36 loc) · 857 Bytes
/
ReformatString.java
File metadata and controls
41 lines (36 loc) · 857 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
32
33
34
35
36
37
38
39
40
41
import java.sql.Ref;
import java.sql.SQLException;
import java.util.Map;
public class ReformatString {
public static void main(String[] args) {
System.out.println(new ReformatString().solution("123456789"));
}
public String solution(String S) {
if (S == null || S.length() <= 0) {
return S;
}
StringBuffer result = new StringBuffer();
int i = 0;
int count = 0;
while (i < S.length()) {
char c = S.charAt(i);
if ('0' <= c && c <= '9') {
result.append(c);
count++;
if (count == 3) {
result.append("-");
count = 0;
}
}
i++;
}
if (count == 0) {
result.delete(result.length() - 1, result.length());
} else if (count == 1) {
result.delete(result.length() - 2, result.length() - 1);
result.insert(result.length() - 2, "-");
}
System.out.println();
return result.toString();
}
}