Skip to content

Add samples for removeSurrounding functions of String and CharSequence #5439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions libraries/stdlib/samples/test/samples/text/strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -814,4 +814,69 @@ class Strings {
// list is converted to a String first and then concatenated with the "Numbers: " string
assertEquals("Numbers: [1, 2, 3]", "Numbers: " + listOf(1, 2, 3))
}

@Sample
fun removeSurroundingPrefixSuffixString() {
val textString = "[content]"
assertPrints(textString.removeSurrounding("[", "]"), "content")
// Does not start with prefix
assertPrints("content]".removeSurrounding("[", "]"), "content]")
// Does not end with suffix
assertPrints("[content".removeSurrounding("[", "]"), "[content")
// Does not start or end with prefix/suffix
assertPrints("content".removeSurrounding("[", "]"), "content")
// Empty content
assertPrints("[]".removeSurrounding("[", "]"), "")
// Different delimiters
assertPrints("<content>".removeSurrounding("[", "]"), "<content>")
}

@Sample
fun removeSurroundingPrefixSuffixCharSequence() {
val textCharSequence: CharSequence = StringBuilder("[content]")
assertPrints(textCharSequence.removeSurrounding("[", "]"), "content")
// Does not start with prefix
assertPrints(StringBuilder("content]").removeSurrounding("[", "]"), "content]")
// Does not end with suffix
assertPrints(StringBuilder("[content").removeSurrounding("[", "]"), "[content")
// Does not start or end with prefix/suffix
assertPrints(StringBuilder("content").removeSurrounding("[", "]"), "content")
// Empty content
assertPrints(StringBuilder("[]").removeSurrounding("[", "]"), "")
// Different delimiters
assertPrints(StringBuilder("<content>").removeSurrounding("[", "]"), "<content>")
}

@Sample
fun removeSurroundingDelimiterString() {
val textString = "***content***"
assertPrints(textString.removeSurrounding("***"), "content")
// Does not end with delimiter
assertPrints("##content".removeSurrounding("##"), "##content")
// Does not start with delimiter
assertPrints("content##".removeSurrounding("##"), "content##")
// No delimiters
assertPrints("content".removeSurrounding("##"), "content")
// Empty content
assertPrints("****".removeSurrounding("**"), "")
// Delimiter is single char, removes only one pair
assertPrints("!!!content!!!".removeSurrounding("!"), "!!content!!")
}

@Sample
fun removeSurroundingDelimiterCharSequence() {
val textCharSequence: CharSequence = StringBuilder("***content***")
assertPrints(textCharSequence.removeSurrounding("***"), "content")
// Does not end with delimiter
assertPrints(StringBuilder("##content").removeSurrounding("##"), "##content")
// Does not start with delimiter
assertPrints(StringBuilder("content##").removeSurrounding("##"), "content##")
// No delimiters
assertPrints(StringBuilder("content").removeSurrounding("##"), "content")
// Empty content
assertPrints(StringBuilder("****").removeSurrounding("**"), "")
// Delimiter is single char, removes only one pair
assertPrints(StringBuilder("!!!content!!!").removeSurrounding("!"), "!!content!!")
}

}
8 changes: 8 additions & 0 deletions libraries/stdlib/src/kotlin/text/Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,8 @@ public fun String.removeSuffix(suffix: CharSequence): String {
* When this char sequence starts with the given [prefix] and ends with the given [suffix],
* returns a new char sequence having both the given [prefix] and [suffix] removed.
* Otherwise, returns a new char sequence with the same characters.
*
* @sample samples.text.Strings.removeSurroundingPrefixSuffixCharSequence
*/
public fun CharSequence.removeSurrounding(prefix: CharSequence, suffix: CharSequence): CharSequence {
if ((length >= prefix.length + suffix.length) && startsWith(prefix) && endsWith(suffix)) {
Expand All @@ -668,6 +670,8 @@ public fun CharSequence.removeSurrounding(prefix: CharSequence, suffix: CharSequ
* Removes from a string both the given [prefix] and [suffix] if and only if
* it starts with the [prefix] and ends with the [suffix].
* Otherwise, returns this string unchanged.
*
* @sample samples.text.Strings.removeSurroundingPrefixSuffixString
*/
public fun String.removeSurrounding(prefix: CharSequence, suffix: CharSequence): String {
if ((length >= prefix.length + suffix.length) && startsWith(prefix) && endsWith(suffix)) {
Expand All @@ -680,13 +684,17 @@ public fun String.removeSurrounding(prefix: CharSequence, suffix: CharSequence):
* When this char sequence starts with and ends with the given [delimiter],
* returns a new char sequence having this [delimiter] removed both from the start and end.
* Otherwise, returns a new char sequence with the same characters.
*
* @sample samples.text.Strings.removeSurroundingDelimiterCharSequence
*/
public fun CharSequence.removeSurrounding(delimiter: CharSequence): CharSequence = removeSurrounding(delimiter, delimiter)

/**
* Removes the given [delimiter] string from both the start and the end of this string
* if and only if it starts with and ends with the [delimiter].
* Otherwise, returns this string unchanged.
*
* @sample samples.text.Strings.removeSurroundingDelimiterString
*/
public fun String.removeSurrounding(delimiter: CharSequence): String = removeSurrounding(delimiter, delimiter)

Expand Down