Skip to content

Commit 727900f

Browse files
committed
Extracting number from string
1 parent 954be11 commit 727900f

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-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,95 @@
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+
}

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)