- Android Development with Kotlin
- Marcin Moskala Igor Wojda
- 143字
- 2021-07-02 18:48:37
Strings
Strings in Kotlin behave in a similar way as in Java, but they have a few nice improvements.
To start to access characters at a specified index, we can use the indexing operator and access characters the same way we access array elements:
val str = "abcd" println (str[1]) // Prints: b
We also have access to various extensions defined in the Kotlin standard library, which make working with strings easier:
val str = "abcd" println(str.reversed()) // Prints: dcba println(str.takeLast(2)) // Prints: cd println("john@test.com".substringBefore("@")) // Prints: john println("john@test.com".startsWith("@")) // Prints: false
This is exactly the same String class as in Java, so these methods are not part of String class. They were defined as extensions. We will learn more about extensions in Chapter 7, Extension Functions and Properties.
Check the String class documentation for a full list of the methods (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/).
推薦閱讀
- Learning Apex Programming
- Django開發(fā)從入門到實踐
- Hands-On JavaScript High Performance
- Learning Probabilistic Graphical Models in R
- Babylon.js Essentials
- 區(qū)塊鏈技術(shù)進階與實戰(zhàn)(第2版)
- 編程可以很簡單
- 新印象:解構(gòu)UI界面設(shè)計
- PostgreSQL Developer's Guide
- Offer來了:Java面試核心知識點精講(框架篇)
- Learning Redux
- HTML5程序開發(fā)范例寶典
- TypeScript High Performance
- Java網(wǎng)絡(luò)編程實用精解
- Delphi Cookbook