- 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.
- JavaScript從入門到精通(微視頻精編版)
- Hands-On Data Structures and Algorithms with JavaScript
- Hands-On JavaScript High Performance
- Silverlight魔幻銀燈
- 數據結構(C語言)
- 快人一步:系統性能提高之道
- Java面向對象程序設計
- Advanced Express Web Application Development
- Web前端應用開發技術
- HTML5開發精要與實例詳解
- Unity 3D腳本編程:使用C#語言開發跨平臺游戲
- RubyMotion iOS Develoment Essentials
- Learning Unreal Engine Game Development
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- Python程序設計:基礎與實踐