Add removePrefix, removeSuffix, removeEnclosing methods.
Deprecate trimLeading/trimTrailing/trim(String) methods in favor of new ones.
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
package kotlin
|
package kotlin
|
||||||
|
|
||||||
/** Returns the string with leading and trailing text matching the given string removed */
|
/** Returns the string with leading and trailing text matching the given string removed */
|
||||||
public fun String.trim(text: String): String = trimLeading(text).trimTrailing(text)
|
deprecated("Use removeEnclosing(text, text) or removePrefix(text).removeSuffix(text)")
|
||||||
|
public fun String.trim(text: String): String = removePrefix(text).removeSuffix(text)
|
||||||
|
|
||||||
/** Returns the string with the prefix and postfix text trimmed */
|
/** Returns the string with the prefix and postfix text trimmed */
|
||||||
public fun String.trim(prefix: String, postfix: String): String = trimLeading(prefix).trimTrailing(postfix)
|
deprecated("Use removeEnclosing(prefix, suffix) or removePrefix(prefix).removeSuffix(suffix)")
|
||||||
|
public fun String.trim(prefix: String, postfix: String): String = removePrefix(prefix).removeSuffix(postfix)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the string with leading and trailing characters matching the [predicate] trimmed.
|
* Returns the string with leading and trailing characters matching the [predicate] trimmed.
|
||||||
@@ -72,29 +74,11 @@ public fun String.trimLeading(vararg chars: Char): String = trimLeading { it in
|
|||||||
*/
|
*/
|
||||||
public fun String.trimTrailing(vararg chars: Char): String = trimTrailing { it in chars }
|
public fun String.trimTrailing(vararg chars: Char): String = trimTrailing { it in chars }
|
||||||
|
|
||||||
/**
|
deprecated("Use removePrefix() instead")
|
||||||
* If this string starts with the given [prefix], returns a copy of this string
|
public fun String.trimLeading(prefix: String): String = removePrefix(prefix)
|
||||||
* with the prefix removed. Otherwise, returns this string.
|
|
||||||
*/
|
|
||||||
public fun String.trimLeading(prefix: String): String {
|
|
||||||
var answer = this
|
|
||||||
if (answer.startsWith(prefix)) {
|
|
||||||
answer = answer.substring(prefix.length())
|
|
||||||
}
|
|
||||||
return answer
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
deprecated("Use removeSuffix() instead")
|
||||||
* If this string ends with the given [postfix], returns a copy of this string
|
public fun String.trimTrailing(postfix: String): String = removeSuffix(postfix)
|
||||||
* with the postfix removed. Otherwise, returns this string.
|
|
||||||
*/
|
|
||||||
public fun String.trimTrailing(postfix: String): String {
|
|
||||||
var answer = this
|
|
||||||
if (answer.endsWith(postfix)) {
|
|
||||||
answer = answer.substring(0, length() - postfix.length())
|
|
||||||
}
|
|
||||||
return answer
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a string with leading and trailing whitespace trimmed.
|
* Returns a string with leading and trailing whitespace trimmed.
|
||||||
@@ -356,6 +340,47 @@ public fun String.removeRange(firstIndex: Int, lastIndex: Int): String {
|
|||||||
*/
|
*/
|
||||||
public fun String.removeRange(range: IntRange): String = removeRange(range.start, range.end + 1)
|
public fun String.removeRange(range: IntRange): String = removeRange(range.start, range.end + 1)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If this string starts with the given [prefix], returns a copy of this string
|
||||||
|
* with the prefix removed. Otherwise, returns this string.
|
||||||
|
*/
|
||||||
|
public fun String.removePrefix(prefix: String): String {
|
||||||
|
if (startsWith(prefix)) {
|
||||||
|
return substring(prefix.length())
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If this string ends with the given [suffix], returns a copy of this string
|
||||||
|
* with the suffix removed. Otherwise, returns this string.
|
||||||
|
*/
|
||||||
|
public fun String.removeSuffix(suffix: String): String {
|
||||||
|
if (endsWith(suffix)) {
|
||||||
|
return substring(0, length() - suffix.length())
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
public fun String.removeEnclosing(prefix: String, suffix: String): String {
|
||||||
|
if (startsWith(prefix) && endsWith(suffix)) {
|
||||||
|
return substring(prefix.length(), length() - suffix.length())
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given [enclosing] string both from the start and the end of this string
|
||||||
|
* if and only if it starts with and ends with the [enclosing].
|
||||||
|
* Otherwise returns this string unchanged.
|
||||||
|
*/
|
||||||
|
public fun String.removeEnclosing(enclosing: String): String = removeEnclosing(enclosing, enclosing)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace part of string before the first occurrence of given delimiter with the [replacement] string.
|
* Replace part of string before the first occurrence of given delimiter with the [replacement] string.
|
||||||
* If the string does not contain the delimiter, returns [missingDelimiterValue] which defaults to the original string.
|
* If the string does not contain the delimiter, returns [missingDelimiterValue] which defaults to the original string.
|
||||||
|
|||||||
@@ -242,4 +242,26 @@ class StringTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test fun removePrefix() {
|
||||||
|
assertEquals("fix", "prefix".removePrefix("pre"), "Removes prefix")
|
||||||
|
assertEquals("prefix", "preprefix".removePrefix("pre"), "Removes prefix once")
|
||||||
|
assertEquals("sample", "sample".removePrefix("value"))
|
||||||
|
assertEquals("sample", "sample".removePrefix(""))
|
||||||
|
}
|
||||||
|
|
||||||
|
test fun removeSuffix() {
|
||||||
|
assertEquals("suf", "suffix".removeSuffix("fix"), "Removes suffix")
|
||||||
|
assertEquals("suffix", "suffixfix".removeSuffix("fix"), "Removes suffix once")
|
||||||
|
assertEquals("sample", "sample".removeSuffix("value"))
|
||||||
|
assertEquals("sample", "sample".removeSuffix(""))
|
||||||
|
}
|
||||||
|
|
||||||
|
test fun removeEnclosing() {
|
||||||
|
assertEquals("value", "<value>".removeEnclosing("<", ">"))
|
||||||
|
assertEquals("<value>", "<<value>>".removeEnclosing("<", ">"), "Removes enclosing once")
|
||||||
|
assertEquals("<value", "<value".removeEnclosing("<", ">"), "Only removes enclosing when both prefix and suffix present")
|
||||||
|
assertEquals("value>", "value>".removeEnclosing("<", ">"), "Only removes enclosing when both prefix and suffix present")
|
||||||
|
assertEquals("value", "value".removeEnclosing("<", ">"))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user