Skip to content

Commit 4fe61ca

Browse files
committed
split a string
1 parent a07eb10 commit 4fe61ca

18 files changed

Lines changed: 486 additions & 0 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.mkyong.string;
2+
3+
public class ConvertStringToInt {
4+
5+
public static void main(String[] args) {
6+
7+
/*String number = "1";
8+
9+
// String to int
10+
int result = Integer.parseInt(number);
11+
12+
// 1
13+
System.out.println(result);*/
14+
15+
String number = "1A";
16+
try {
17+
int result = Integer.parseInt(number);
18+
System.out.println(result);
19+
} catch (NumberFormatException e) {
20+
//do something for the exception.
21+
System.err.println("Invalid number format : " + number);
22+
}
23+
24+
}
25+
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mkyong.string;
2+
3+
public class ConvertStringToInt2 {
4+
5+
public static void main(String[] args) {
6+
7+
// 2147483647
8+
System.out.println(Integer.MAX_VALUE);
9+
10+
String number = "2147483648";
11+
try {
12+
int result = Integer.parseInt(number);
13+
System.out.println(result);
14+
} catch (NumberFormatException e) {
15+
//do something for the exception.
16+
System.err.println("Invalid number format : " + number);
17+
}
18+
19+
}
20+
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.mkyong.string;
2+
3+
public class ConvertStringToInteger {
4+
5+
public static void main(String[] args) {
6+
7+
/*String number = "99";
8+
9+
// String to integer
10+
Integer result = Integer.valueOf(number);
11+
12+
// 99
13+
System.out.println(result);*/
14+
15+
String number = "D99";
16+
try {
17+
Integer result = Integer.valueOf(number);
18+
System.out.println(result);
19+
} catch (NumberFormatException e) {
20+
//do something for the exception.
21+
System.err.println("Invalid number format : " + number);
22+
}
23+
24+
}
25+
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.mkyong.string;
2+
3+
import java.util.Optional;
4+
5+
public class ConvertStringToIntegerJava8 {
6+
7+
public static void main(String[] args) {
8+
9+
String number = "99";
10+
11+
Optional<Integer> result = convertStringToInteger(number);
12+
13+
if (result.isPresent()) {
14+
System.out.println(result.get());
15+
} else {
16+
System.err.println("Unable to convert the number : " + number);
17+
}
18+
19+
}
20+
21+
private static Optional<Integer> convertStringToInteger(String input) {
22+
23+
if (input == null) return Optional.empty();
24+
25+
input = input.trim();
26+
27+
if (input.isEmpty()) return Optional.empty();
28+
29+
try {
30+
return Optional.of(Integer.valueOf(input));
31+
} catch (NumberFormatException e) {
32+
return Optional.empty();
33+
}
34+
35+
}
36+
37+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mkyong.string.split;
2+
3+
public class StringSplit {
4+
5+
public static void main(String[] args) {
6+
7+
String phone = "012-3456789";
8+
String[] output = phone.split("-");
9+
10+
System.out.println(output.length); // 2
11+
12+
String part1 = output[0]; // 012
13+
String part2 = output[1]; // 3456789
14+
15+
System.out.println(part1); // 012
16+
System.out.println(part2); // 3456789
17+
18+
}
19+
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mkyong.string.split;
2+
3+
public class StringSplitContains {
4+
5+
public static void main(String[] args) {
6+
7+
String phone = "012-3456789";
8+
9+
if (phone.contains("-")) {
10+
11+
String[] output = phone.split("-");
12+
System.out.println(output[0]); // 012
13+
System.out.println(output[1]); // 3456789
14+
15+
} else {
16+
throw new IllegalArgumentException("String " + phone + " does not contain -");
17+
}
18+
19+
}
20+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.mkyong.string.split;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class StringSplitJava8 {
8+
9+
public static void main(String[] args) {
10+
11+
String phone = "012-3456789";
12+
13+
/*List<String> output = Pattern.compile("-")
14+
.splitAsStream(phone)
15+
.collect(Collectors.toList());*/
16+
17+
List<String> output = Arrays.stream(phone.split("-"))
18+
.collect(Collectors.toList());
19+
20+
System.out.println(output);
21+
22+
23+
}
24+
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.mkyong.string.split;
2+
3+
public class StringSplitLimit {
4+
5+
public static void main(String[] args) {
6+
7+
String phone = "012-345-678-9";
8+
String[] output = phone.split("-", 3);
9+
10+
System.out.println(output.length); // 3
11+
12+
String part1 = output[0]; // 012
13+
String part2 = output[1]; // 345
14+
String part3 = output[2]; // 678-9
15+
16+
System.out.println(part1);
17+
System.out.println(part2);
18+
System.out.println(part3);
19+
20+
}
21+
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mkyong.string.split;
2+
3+
public class StringSplitLookAround {
4+
5+
public static void main(String[] args) {
6+
7+
String phone = "012-3456789";
8+
9+
if (phone.contains("-")) {
10+
11+
// positive lookahead, split char at right-hand side
12+
String[] output = phone.split("(?=-)");
13+
System.out.println(output[0]); // 012
14+
System.out.println(output[1]); // -3456789
15+
16+
// positive lookbehind, split char at left-hand side
17+
String[] output2 = phone.split("(?<=-)");
18+
System.out.println(output2[0]); // 012-
19+
System.out.println(output2[1]); // 3456789
20+
21+
} else {
22+
throw new IllegalArgumentException("String " + phone + " does not contain -");
23+
}
24+
25+
}
26+
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mkyong.string.split;
2+
3+
public class StringSplitMultiDelimiters {
4+
5+
public static void main(String[] args) {
6+
7+
String dir = "apple|9|1.88;2.78|0#10";
8+
String[] output = dir.split("[|;#]");
9+
10+
System.out.println(output.length); // 6
11+
12+
for (String s : output) {
13+
System.out.println(s);
14+
}
15+
16+
/*
17+
System.out.println(output[0]); // apple
18+
System.out.println(output[1]); // 9
19+
System.out.println(output[2]); // 1.88
20+
System.out.println(output[3]); // 2.78
21+
System.out.println(output[4]); // 0
22+
System.out.println(output[5]); // 10
23+
*/
24+
25+
}
26+
27+
}

0 commit comments

Comments
 (0)