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

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

主站蜘蛛池模板: 凤翔县| 涞源县| 三河市| 栾城县| 紫阳县| 平顶山市| 台南县| 远安县| 肇源县| 东源县| 芜湖市| 永清县| 略阳县| 南宫市| 青州市| 新丰县| 壤塘县| 台南市| 贵溪市| 碌曲县| 子长县| 工布江达县| 宕昌县| 保定市| 和政县| 阿图什市| 东兰县| 新宁县| 孟津县| 孝义市| 六盘水市| 云南省| 平谷区| 乡城县| 大庆市| 新龙县| 涟水县| 赣榆县| 曲阳县| 孝昌县| 边坝县|