Skip to content

Instantly share code, notes, and snippets.

@msymt
Last active May 26, 2020 08:55
Show Gist options
  • Save msymt/9ce663e0fcf9058c9a6d2290fdc5c6b5 to your computer and use it in GitHub Desktop.
Save msymt/9ce663e0fcf9058c9a6d2290fdc5c6b5 to your computer and use it in GitHub Desktop.
拡張プロパティ
val String.lastChar: Char
get() = get(length - 1)
var StringBuilder.lastChar: Char
get() = get(length - 1)
set(value: Char) {
this.setCharAt(length - 1, value)
}
/*
>>>println("Kotlin!".lastChar)
!
>>>val sb = StringBuilder("Kotlin?")
>>>println(sb)
Kotlin?
>>>sb.lastChar = '!'
>>>println(sb)
Kotlin!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment