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

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/).
主站蜘蛛池模板: 新和县| 依兰县| 扬中市| 抚顺县| 虞城县| 许昌市| 会理县| 浮山县| 峡江县| 漯河市| 东乡县| 乐昌市| 香格里拉县| 阳江市| 沅江市| 夏邑县| 长汀县| 阿瓦提县| 客服| 宣化县| 泾源县| 太仓市| 瑞丽市| 沈丘县| 临漳县| 临朐县| 大英县| 怀安县| 永城市| 分宜县| 闽侯县| 道真| 通化县| 乃东县| 丰顺县| 柳河县| 梧州市| 丹江口市| 胶州市| 湟中县| 裕民县|