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

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

主站蜘蛛池模板: 屯昌县| 汪清县| 吕梁市| 凯里市| 独山县| 边坝县| 天水市| 海林市| 固阳县| 阳高县| 浪卡子县| 甘南县| 山东| 洛扎县| 重庆市| 资兴市| 凤台县| 虞城县| 抚远县| 江口县| 辉县市| 互助| 沅江市| 杭锦旗| 蒙阴县| 定州市| 桂阳县| 中超| 临湘市| 承德县| 新密市| 山阳县| 芜湖县| 桐梓县| 东光县| 勃利县| 泰宁县| 常宁市| 莎车县| 怀远县| 集贤县|