1
+ package com.baeldung.extractnumber
2
+
3
+ import org.junit.jupiter.api.Assertions
4
+ import org.junit.jupiter.params.ParameterizedTest
5
+ import org.junit.jupiter.params.provider.CsvSource
6
+
7
+ fun extractUsingRegex (str : String ): Long? {
8
+ return Regex (" \\ d+" ).find(str)?.value?.toLongOrNull()
9
+ }
10
+
11
+ fun extractMultipleUsingRegex (str : String ): List <Long > {
12
+ return Regex (" \\ d+" ).findAll(str).map { it.value.toLong() }.toList()
13
+ }
14
+
15
+ fun extractNumbersUsingSplitAndRegex (str : String ): List <Long > {
16
+ return str.split(Regex (" \\ D+" ))
17
+ .filter { it.isNotBlank() }
18
+ .map { it.toLong() }
19
+ }
20
+
21
+ fun extractNumbersUsingLoop (str : String ): List <Long > {
22
+ val numbers = mutableListOf<Long >()
23
+ val currentNumber = StringBuilder ()
24
+
25
+ for (char in str) {
26
+ if (char.isDigit()) {
27
+ currentNumber.append(char)
28
+ } else if (currentNumber.isNotEmpty()) {
29
+ numbers.add(currentNumber.toString().toLong())
30
+ currentNumber.clear()
31
+ }
32
+ }
33
+
34
+ if (currentNumber.isNotEmpty()) {
35
+ numbers.add(currentNumber.toString().toLong())
36
+ }
37
+
38
+ return numbers
39
+ }
40
+
41
+
42
+ class NumberExtractUnitTest {
43
+
44
+ @ParameterizedTest
45
+ @CsvSource(
46
+ " string with 123 and 333 in the text, 123" ,
47
+ " another string with 456 and 789, 456" ,
48
+ " string 123-234, 123" ,
49
+ " no numbers," ,
50
+ )
51
+ fun `extract first occurrence of number from string using regex` (str : String , expected : String? ) {
52
+ val number = extractUsingRegex(str)
53
+ Assertions .assertEquals(number, expected?.toLongOrNull())
54
+ }
55
+
56
+ @ParameterizedTest
57
+ @CsvSource(
58
+ " string with 123 and 333 in the text, 123:333" ,
59
+ " another string with 456 and 789, 456:789" ,
60
+ " string 123-234, 123:234" ,
61
+ " no numbers," ,
62
+ )
63
+ fun `extract all occurrences of numbers from string using regex` (str : String , expected : String? ) {
64
+ val numbers = extractMultipleUsingRegex(str)
65
+ val expectedList = expected?.split(" :" )?.map { it.toLong() } ? : emptyList()
66
+ Assertions .assertEquals(expectedList, numbers)
67
+ }
68
+
69
+ @ParameterizedTest
70
+ @CsvSource(
71
+ " string with 123 and 333 in the text, 123:333" ,
72
+ " another string with 456 and 789, 456:789" ,
73
+ " string 123-234, 123:234" ,
74
+ " no numbers," ,
75
+ )
76
+ fun `extract all occurrences of numbers from string using split and regex` (str : String , expected : String? ) {
77
+ val numbers = extractNumbersUsingSplitAndRegex(str)
78
+ val expectedList = expected?.split(" :" )?.map { it.toLong() } ? : emptyList()
79
+ Assertions .assertEquals(expectedList, numbers)
80
+ }
81
+
82
+ @ParameterizedTest
83
+ @CsvSource(
84
+ " string with 123 and 333 in the text, 123:333" ,
85
+ " another string with 456 and 789, 456:789" ,
86
+ " string 123-234, 123:234" ,
87
+ " no numbers," ,
88
+ )
89
+ fun `extract all occurrences of numbers from string using loop` (str : String , expected : String? ) {
90
+ val numbers = extractNumbersUsingLoop(str)
91
+ val expectedList = expected?.split(" :" )?.map { it.toLong() } ? : emptyList()
92
+ Assertions .assertEquals(expectedList, numbers)
93
+ }
94
+
95
+ }
0 commit comments