Skip to content

Instantly share code, notes, and snippets.

@automationhacks
Created June 16, 2019 12:25
Show Gist options
  • Save automationhacks/e40512afdd071f0d04cacfc8f457c7b4 to your computer and use it in GitHub Desktop.
Save automationhacks/e40512afdd071f0d04cacfc8f457c7b4 to your computer and use it in GitHub Desktop.
Final code with all refactorings done
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