Created
June 16, 2019 12:25
-
-
Save automationhacks/e40512afdd071f0d04cacfc8f457c7b4 to your computer and use it in GitHub Desktop.
Final code with all refactorings done
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package _03_functions | |
fun String.getFirstWord(separator: String = " "): String { | |
return this.split(separator)[0] | |
} | |
// Example of extension properties | |
// In this case on top of a String class | |
val String.lastWord: String | |
get() { | |
return this.split("")[1] | |
} | |
fun main() { | |
val first = "Jane Doe".getFirstWord() | |
val last = "John Doe".lastWord | |
println("First word: $first") | |
println("Last word (Using extension properties) $last") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment