added capitalize/decapitalize of strings and isUpper/LowerCase helpers to Char

This commit is contained in:
James Strachan
2012-05-23 08:28:02 +01:00
parent 6f921a63d7
commit a4991534b4
3 changed files with 42 additions and 0 deletions
+9
View File
@@ -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)
+18
View File
@@ -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