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

  • 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.

主站蜘蛛池模板: 宁武县| 那曲县| 当阳市| 綦江县| 大名县| 桦甸市| 南靖县| 广南县| 阿荣旗| 文水县| 靖江市| 资溪县| 分宜县| 商水县| 仙游县| 湘潭县| 开封市| 新安县| 平乡县| 威信县| 比如县| 石河子市| 科尔| 渑池县| 金坛市| 讷河市| 湟中县| 彭山县| 全州县| 瑞昌市| 侯马市| 铜梁县| 忻州市| 东兰县| 太原市| 怀柔区| 通辽市| 新丰县| 沿河| 介休市| 凤山县|