Skip to content

Commit d6ad8ca

Browse files
Add replace and remove with varargs parameter
1 parent 659ae0a commit d6ad8ca

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Added
66

7+
- `String.replace(vararg oldToNewValues: Pair<String, String>): String`
8+
- `String.remove(vararg values: String): String`
9+
710
### Changed
811

912
### Deprecated

kotlin-stdlib/api/kotlin-stdlib.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ public final class com/javiersc/kotlin/stdlib/StringsKt {
168168
public static final fun notContain (Ljava/lang/CharSequence;Lkotlin/text/Regex;)Z
169169
public static synthetic fun notContain$default (Ljava/lang/CharSequence;Ljava/lang/CharSequence;ZILjava/lang/Object;)Z
170170
public static final fun remove (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/String;
171+
public static final fun remove (Ljava/lang/String;[Ljava/lang/String;)Ljava/lang/String;
171172
public static synthetic fun remove$default (Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Ljava/lang/String;
172173
public static final fun removeDuplicateEmptyLines (Ljava/lang/String;)Ljava/lang/String;
174+
public static final fun replace (Ljava/lang/String;[Lkotlin/Pair;)Ljava/lang/String;
173175
}
174176

kotlin-stdlib/build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ hubdle {
66
config {
77
explicitApi()
88
publishing()
9+
languageSettings {
10+
rawConfig {
11+
languageSettings {
12+
optIn("kotlin.contracts.ExperimentalContracts")
13+
}
14+
}
15+
}
916
}
1017
kotlin {
1118
multiplatform {

kotlin-stdlib/commonMain/kotlin/com/javiersc/kotlin/stdlib/Strings.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
@file:OptIn(ExperimentalContracts::class)
2-
31
package com.javiersc.kotlin.stdlib
42

5-
import kotlin.contracts.ExperimentalContracts
63
import kotlin.contracts.contract
74

5+
public fun String.replace(vararg oldToNewValues: Pair<String, String>): String {
6+
var result = this
7+
for ((oldValue, newValue) in oldToNewValues) {
8+
result = result.replace(oldValue, newValue)
9+
}
10+
return result
11+
}
12+
813
/**
914
* Returns a new string obtained by removing all occurrences of the [value] substring in this string
1015
*/
1116
public inline fun String.remove(value: String, ignoreCase: Boolean = false): String =
1217
replace(oldValue = value, newValue = "", ignoreCase = ignoreCase)
1318

19+
public fun String.remove(vararg values: String): String {
20+
var result = this
21+
for (value in values) {
22+
result = result.replace(value, "")
23+
}
24+
return result
25+
}
26+
1427
public inline fun CharSequence?.isNotNullNorBlank(): Boolean {
1528
contract { returns(true) implies (this@isNotNullNorBlank != null) }
1629

kotlin-stdlib/commonTest/kotlin/com/javiersc/kotlin/stdlib/StringsTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ class StringsTest {
1212
fun string_remove() {
1313
"Hello, World".remove("Hello, ").shouldBe("World")
1414
"Hello, World".remove("bla").shouldBe("Hello, World")
15+
"Hello, World".remove("llo", "rld").shouldBe("He, Wo")
16+
}
17+
18+
@Test
19+
fun string_replace() {
20+
"Hello, World".replace("ello" to "ELLO", "orld" to "ORLD").shouldBe("HELLO, WORLD")
1521
}
1622

1723
@Test

0 commit comments

Comments
 (0)