Skip to content

Commit 3a4ec46

Browse files
authored
Merge pull request #879 from yadavan88/extract-numbers
Extracting number from string
2 parents 3d76804 + d197e55 commit 3a4ec46

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Core Kotlin Strings
2+
3+
This module contains articles about core Kotlin strings.
4+
5+
### Relevant Articles
6+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-kotlin-strings-5</artifactId>
7+
<name>core-kotlin-strings-5</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>core-kotlin-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.junit.jupiter</groupId>
19+
<artifactId>junit-jupiter-params</artifactId>
20+
<version>5.10.2</version>
21+
<scope>test</scope>
22+
</dependency>
23+
</dependencies>
24+
25+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.baeldung.extractnumber
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.api.Test
5+
import org.junit.jupiter.params.ParameterizedTest
6+
import org.junit.jupiter.params.provider.CsvSource
7+
import java.math.BigInteger
8+
9+
fun extractUsingRegex(str: String): BigInteger? {
10+
return Regex("\\d+").find(str)?.value?.toBigIntegerOrNull()
11+
}
12+
13+
fun extractMultipleUsingRegex(str: String): List<BigInteger> {
14+
return Regex("\\d+").findAll(str).map { it.value.toBigInteger() }.toList()
15+
}
16+
17+
fun extractNumbersUsingSplitAndRegex(str: String): List<BigInteger> {
18+
return str.split(Regex("\\D+"))
19+
.filter { it.isNotBlank() }
20+
.map { it.toBigInteger() }
21+
}
22+
23+
fun extractNumbersUsingLoop(str: String): List<BigInteger> {
24+
val numbers = mutableListOf<BigInteger>()
25+
val currentNumber = StringBuilder()
26+
27+
for (char in str) {
28+
if (char.isDigit()) {
29+
currentNumber.append(char)
30+
} else if (currentNumber.isNotEmpty()) {
31+
numbers.add(currentNumber.toString().toBigInteger())
32+
currentNumber.clear()
33+
}
34+
}
35+
36+
if (currentNumber.isNotEmpty()) {
37+
numbers.add(currentNumber.toString().toBigInteger())
38+
}
39+
40+
return numbers
41+
}
42+
43+
44+
class NumberExtractUnitTest {
45+
46+
@ParameterizedTest
47+
@CsvSource(
48+
"string with 123 and 333 in the text, 123",
49+
"another string with 456 and 789, 456",
50+
"string 123-234, 123",
51+
"no numbers,",
52+
"3 4 50 numbers6, 3",
53+
"123456789large numbers0, 123456789",
54+
"91234567891011121314151617181920number,91234567891011121314151617181920"
55+
)
56+
fun `extract first occurrence of number from string using regex`(str: String, expected: String?) {
57+
val number = extractUsingRegex(str)
58+
Assertions.assertEquals(number, expected?.toBigIntegerOrNull())
59+
}
60+
61+
@ParameterizedTest
62+
@CsvSource(
63+
"string with 123 and 333 in the text, 123:333",
64+
"another string with 456 and 789, 456:789",
65+
"string 123-234, 123:234",
66+
"no numbers,",
67+
"3 4 50 numbers6, 3:4:50:6",
68+
"123456789large numbers0, 123456789:0",
69+
"91234567891011121314151617181920number,91234567891011121314151617181920"
70+
)
71+
fun `extract all occurrences of numbers from string using regex`(str: String, expected: String?) {
72+
val numbers = extractMultipleUsingRegex(str)
73+
val expectedList = expected?.split(":")?.map { it.toBigInteger() } ?: emptyList()
74+
Assertions.assertEquals(expectedList, numbers)
75+
}
76+
77+
@ParameterizedTest
78+
@CsvSource(
79+
"string with 123 and 333 in the text, 123:333",
80+
"another string with 456 and 789, 456:789",
81+
"string 123-234, 123:234",
82+
"no numbers,",
83+
"3 4 50 numbers6, 3:4:50:6",
84+
"123456789large numbers0, 123456789:0",
85+
"91234567891011121314151617181920number,91234567891011121314151617181920"
86+
)
87+
fun `extract all occurrences of numbers from string using split and regex`(str: String, expected: String?) {
88+
val numbers = extractNumbersUsingSplitAndRegex(str)
89+
val expectedList = expected?.split(":")?.map { it.toBigInteger() } ?: emptyList()
90+
Assertions.assertEquals(expectedList, numbers)
91+
}
92+
93+
@ParameterizedTest
94+
@CsvSource(
95+
"string with 123 and 333 in the text, 123:333",
96+
"another string with 456 and 789, 456:789",
97+
"string 123-234, 123:234",
98+
"no numbers,",
99+
"3 4 50 numbers6, 3:4:50:6",
100+
"123456789large numbers0, 123456789:0",
101+
"91234567891011121314151617181920number,91234567891011121314151617181920"
102+
)
103+
fun `extract all occurrences of numbers from string using loop`(str: String, expected: String?) {
104+
val numbers = extractNumbersUsingLoop(str)
105+
val expectedList = expected?.split(":")?.map { it.toBigInteger() } ?: emptyList()
106+
Assertions.assertEquals(expectedList, numbers)
107+
}
108+
109+
@Test
110+
fun `check empty string scenario for loop`() {
111+
val numbers = extractNumbersUsingLoop("")
112+
Assertions.assertIterableEquals(numbers, listOf<BigInteger>())
113+
}
114+
115+
@Test
116+
fun `check empty string scenario for split`() {
117+
val numbers = extractNumbersUsingSplitAndRegex("")
118+
Assertions.assertIterableEquals(numbers, listOf<BigInteger>())
119+
}
120+
121+
@Test
122+
fun `check empty string scenario for regex`() {
123+
val numbers = extractMultipleUsingRegex("")
124+
Assertions.assertIterableEquals(numbers, listOf<BigInteger>())
125+
}
126+
127+
}

core-kotlin-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<module>core-kotlin-strings-2</module>
6565
<module>core-kotlin-strings-3</module>
6666
<module>core-kotlin-strings-4</module>
67+
<module>core-kotlin-strings-5</module>
6768
<module>core-kotlin-string-conversions</module>
6869
<module>core-kotlin-files</module>
6970
<module>core-kotlin-operators</module>

0 commit comments

Comments
 (0)