added capitalize/decapitalize of strings and isUpper/LowerCase helpers to Char
This commit is contained in:
@@ -25,3 +25,12 @@ public inline fun Char.isJavaLetterOrDigit(): Boolean = Character.isJavaLetterOr
|
||||
*/
|
||||
public inline fun Char.isWhitespace(): Boolean = Character.isWhitespace(this)
|
||||
|
||||
/**
|
||||
* Returns true if this character is upper case
|
||||
*/
|
||||
public inline fun Char.isUpperCase(): Boolean = Character.isUpperCase(this)
|
||||
|
||||
/**
|
||||
* Returns true if this character is lower case
|
||||
*/
|
||||
public inline fun Char.isLowerCase(): Boolean = Character.isLowerCase(this)
|
||||
|
||||
@@ -35,6 +35,24 @@ public inline fun String.trimLeading(prefix: String): String {
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of this string capitalised if it is not empty or already starting with an uppper case letter, otherwise returns this
|
||||
*
|
||||
* @includeFunctionBody ../../test/StringTest.kt capitalize
|
||||
*/
|
||||
public inline fun String.capitalize(): String {
|
||||
return if (notEmpty() && charAt(0).isLowerCase()) substring(0, 1).toUpperCase() + substring(1) else this
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of this string with the first letter lower case if it is not empty or already starting with a lower case letter, otherwise returns this
|
||||
*
|
||||
* @includeFunctionBody ../../test/StringTest.kt decapitalize
|
||||
*/
|
||||
public inline fun String.decapitalize(): String {
|
||||
return if (notEmpty() && charAt(0).isUpperCase()) substring(0, 1).toLowerCase() + substring(1) else this
|
||||
}
|
||||
|
||||
/** Returns the string with the trailing postfix of this string removed */
|
||||
public inline fun String.trimTrailing(postfix: String): String {
|
||||
var answer = this
|
||||
|
||||
@@ -78,4 +78,19 @@ class StringTest {
|
||||
assertFalse("".endsWith('a'))
|
||||
}
|
||||
|
||||
test fun capitalize() {
|
||||
assertEquals("A", "A".capitalize())
|
||||
assertEquals("A", "a".capitalize())
|
||||
assertEquals("Abcd", "abcd".capitalize())
|
||||
assertEquals("Abcd", "Abcd".capitalize())
|
||||
}
|
||||
|
||||
test fun decapitalize() {
|
||||
assertEquals("a", "A".decapitalize())
|
||||
assertEquals("a", "a".decapitalize())
|
||||
assertEquals("abcd", "abcd".decapitalize())
|
||||
assertEquals("abcd", "Abcd".decapitalize())
|
||||
assertEquals("uRL", "URL".decapitalize())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user