forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStrings.java
More file actions
48 lines (35 loc) · 1.17 KB
/
Copy pathStrings.java
File metadata and controls
48 lines (35 loc) · 1.17 KB
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
42
43
44
45
46
47
48
package modern.challenge;
public final class Strings {
private Strings() {
throw new AssertionError("Cannot be instantiated");
}
// Note: For Unicode supplementary characters use codePointAt() instead of charAt()
// and codePoints() instead of chars()
public static boolean containsOnlyDigitsV1(String str) {
if (str == null || str.isBlank()) {
// or throw IllegalArgumentException
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
public static boolean containsOnlyDigitsV2(String str) {
if (str == null || str.isBlank()) {
// or throw IllegalArgumentException
return false;
}
return str.matches("[0-9]+");
}
public static boolean containsOnlyDigitsV3(String str) {
if (str == null || str.isBlank()) {
// or throw IllegalArgumentException
return false;
}
return !str.chars()
.anyMatch(n -> !Character.isDigit(n));
}
}