# How to apply template to a string returned from a function

**URL:** https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942
**Category:** Uncategorized
**Created:** [January 17, 2013, 9:24am UTC](https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942 "2013-01-17T09:24:28Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dodyg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dodyg/32/1451_2.png) [@dodyg](https://discuss.kotlinlang.org/u/dodyg)
#### Post date: [January 17, 2013, 9:24am UTC](https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942/1 "2013-01-17T09:24:28Z")

</div>

In Android text can be put on a resource file like

\<string name="download\_all"\>Download all $name\</string\>

and it can be retrieved by a call such as getString(download\_all)

The problem is&nbsp;&nbsp;the template expression doesn’t work in this situation ($name is not evaluated).

---

<div class="post-metadata">

### Author: ![programingjd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/programingjd/32/1526_2.png) [@programingjd](https://discuss.kotlinlang.org/u/programingjd)
#### Post date: [January 17, 2013, 9:42am UTC](https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942/2 "2013-01-17T09:42:25Z")

</div>

That resource string could be used anywhere. How would the compiler know what $name is referring to?

In this case, I think you should use:  
`MessageFormat.format(getString(R.string.download_all), name)`  
or  
`String.format(getString(R.string.download_all), name)`

---

<div class="post-metadata">

### Author: ![dodyg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dodyg/32/1451_2.png) [@dodyg](https://discuss.kotlinlang.org/u/dodyg)
#### Post date: [January 17, 2013, 9:58am UTC](https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942/3 "2013-01-17T09:58:22Z")

</div>

Unfortunately these calls do not work

&nbsp;&nbsp;\<string name="edit\_sources\_of\_collection"\>Edit sources of '$collectionTitle'\</string\>

String.format(getString(R.string.edit\_sources\_of\_collection)!!, collectionTitle)

generates error

Please specify constructor invocation; classifier ‘String’ does not have a class object

“”.format(getString(R.string.edit\_sources\_of\_collection)!!, collectionTitle)

  
generates

Edit sources of ‘$collectionTitle’

Ditto MessageFormat.formatgetString(R.string.edit\_sources\_of\_collection)!!, collectionTitle)

---

<div class="post-metadata">

### Author: ![programingjd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/programingjd/32/1526_2.png) [@programingjd](https://discuss.kotlinlang.org/u/programingjd)
#### Post date: [January 17, 2013, 10:04am UTC](https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942/4 "2013-01-17T10:04:19Z")

</div>

I am sorry, I should have mentioned that you need to change your definition of the string too.

If you are going to use MessageFormat, you need to replace $variableName with {0}  
If you are going to use String.format, you need to replace $variableName with %1$s

Both of them indicate that it should be replaced by the first argument of the format method.

---

<div class="post-metadata">

### Author: ![programingjd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/programingjd/32/1526_2.png) [@programingjd](https://discuss.kotlinlang.org/u/programingjd)
#### Post date: [January 17, 2013, 10:06am UTC](https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942/5 "2013-01-17T10:06:47Z")

</div>

For calling String.format, you have to preprend java.lang to let kotlin know that the method is on java.lang.String and not jet.String.

java.lang.String.format(formatString, arg1, arg2, …)

---

<div class="post-metadata">

### Author: ![dodyg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dodyg/32/1451_2.png) [@dodyg](https://discuss.kotlinlang.org/u/dodyg)
#### Post date: [January 17, 2013, 10:12am UTC](https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942/6 "2013-01-17T10:12:40Z")

</div>

Ah ok. I am wondering whether there is a method that I can use to use the named templated expression instead of the positional one.

&nbsp;&nbsp;\<string name="edit\_sources\_of\_collection"\>Edit sources of {0} \</string\>

is not very descriptive.

---

<div class="post-metadata">

### Author: ![programingjd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/programingjd/32/1526_2.png) [@programingjd](https://discuss.kotlinlang.org/u/programingjd)
#### Post date: [January 17, 2013, 10:22am UTC](https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942/7 "2013-01-17T10:22:02Z")

</div>

With MessageFormat, I think {0,,someName} works and is an easy way to attach a name for descriptive purposes.

---

<div class="post-metadata">

### Author: ![dodyg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dodyg/32/1451_2.png) [@dodyg](https://discuss.kotlinlang.org/u/dodyg)
#### Post date: [January 17, 2013, 12:11pm UTC](https://discuss.kotlinlang.org/t/how-to-apply-template-to-a-string-returned-from-a-function/942/8 "2013-01-17T12:11:43Z")

</div>

Unfortunately the doc don't support this [http://docs.oracle.com/javase/1.4.2/docs/api/java/text/MessageFormat.html](http://docs.oracle.com/javase/1.4.2/docs/api/java/text/MessageFormat.html)
