- 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/).
推薦閱讀
- JavaScript 從入門(mén)到項(xiàng)目實(shí)踐(超值版)
- WSO2 Developer’s Guide
- Python從入門(mén)到精通(精粹版)
- SQL Server 2016數(shù)據(jù)庫(kù)應(yīng)用與開(kāi)發(fā)習(xí)題解答與上機(jī)指導(dǎo)
- 零基礎(chǔ)輕松學(xué)SQL Server 2016
- 微信小程序項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)
- SQL Server從入門(mén)到精通(第3版)
- Express Web Application Development
- Flowable流程引擎實(shí)戰(zhàn)
- Lift Application Development Cookbook
- Python自然語(yǔ)言理解:自然語(yǔ)言理解系統(tǒng)開(kāi)發(fā)與應(yīng)用實(shí)戰(zhàn)
- 測(cè)試架構(gòu)師修煉之道:從測(cè)試工程師到測(cè)試架構(gòu)師
- Drupal 8 Development Cookbook(Second Edition)
- ArcPy and ArcGIS(Second Edition)
- Java 7 Concurrency Cookbook