官术网_书友最值得收藏!

  • 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/).
主站蜘蛛池模板: 新蔡县| 海阳市| 镇坪县| 信阳市| 宜宾市| 彭山县| 白水县| 华宁县| 凤庆县| 平顶山市| 招远市| 康保县| 泾川县| 荔浦县| 承德县| 镇康县| 邵武市| 拉萨市| 宁化县| 青海省| 化隆| 津市市| 秦安县| 邢台县| 新民市| 连平县| 衡东县| 天峻县| 仪陇县| 瑞金市| 大埔县| 福建省| 东平县| 金山区| 宁明县| 民县| 中卫市| 循化| 光泽县| 革吉县| 克山县|