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

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.

主站蜘蛛池模板: 苏尼特左旗| 汉寿县| 梅州市| 昌平区| 库伦旗| 大连市| 肇源县| 广宗县| 育儿| 梁河县| 林周县| 绿春县| 巴楚县| 凉山| 邯郸市| 天津市| 鲁甸县| 邵阳市| 锦州市| 综艺| 濮阳市| 磴口县| 隆昌县| 嘉荫县| 车险| 慈利县| 微博| 肇庆市| 徐汇区| 汕尾市| 千阳县| 永和县| 来凤县| 衡水市| 伊川县| 东方市| 灵台县| 吴江市| 新蔡县| 宁远县| 喜德县|