- Kotlin Blueprints
- Ashish Belagali Hardik Trivedi Akshay Chordiya
- 163字
- 2021-07-02 21:50:22
String interpolation
A string is a most commonly and heavily used class. String literal is also an instance of the string class. String interpolation has the most beautiful features in Kotlin.
Interpolation is basically smart string concatenation. A typical string concatenation can look as follows:
val name="Kotlin Blueprints"
println("Name $name")
In Android, there can be many cases where we do string concatenation and string interpolation can help us.
Remember! We have to display dynamic labels on TextView countless times in the app. The code for that using string interpolation can look as follows:
txtFollowers.text = "$followersCount
${getString(R.string.cnt_followers)}"
txtFriendsCount.text = "$friendsCount
${getString(R.string.cnt_friends)}"
txtTweets.text = "${status.retweetCount}
${getString(R.string.cnt_retweet)}"
If you observe the preceding code snippet, we actually use both objects and properties as well.
String interpolation internally uses the StringBuilder class to achieve this. Also, we observed that there is no caching performed. Every time the statement executes a new object gets created. Let's hope that Kotlin will fix this soon in coming versions.
- Practical Data Analysis Cookbook
- C程序設計簡明教程(第二版)
- Blender 3D Incredible Machines
- Visual Basic學習手冊
- 青少年Python編程入門
- C語言程序設計
- HTML5從入門到精通 (第2版)
- Getting Started with LLVM Core Libraries
- Extending Puppet(Second Edition)
- ASP.NET Core 2 Fundamentals
- Programming with CodeIgniterMVC
- JavaEE架構與程序設計
- 嵌入式C編程實戰
- Flink核心技術:源碼剖析與特性開發
- Android應用程序設計