- 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.
- Docker技術入門與實戰(第3版)
- Cassandra Design Patterns(Second Edition)
- Python高級機器學習
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- JS全書:JavaScript Web前端開發指南
- 零基礎學Java程序設計
- 深入分布式緩存:從原理到實踐
- 編程可以很簡單
- HTML+CSS+JavaScript編程入門指南(全2冊)
- WordPress Search Engine Optimization(Second Edition)
- Java程序設計實用教程(第2版)
- jQuery Mobile Web Development Essentials(Second Edition)
- Java Web開發基礎與案例教程
- Clojure Data Structures and Algorithms Cookbook
- Tkinter GUI Application Development Blueprints