5

I am trying to concatenate an Int with a String such that the output is a String but not sure how to go about it.

Here's my code so far:

val myMessage = context.getString(R.string.mymessage)

where myMessage is a String.

Now I want to append an Int which is it.myinfo.codeid.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121

3 Answers3

15

You can use string templates:

"${context.getString(R.string.mymessage)} ${it.myinfo.codeid}"
Salem
  • 13,516
  • 4
  • 51
  • 70
  • That’s the way to go – s1m0nw1 Feb 15 '18 at 06:48
  • @Moira any idea about this one ? https://stackoverflow.com/questions/49302694/how-do-i-detect-skype-telegram-whatsapp-calls-when-my-messenger-app-is-in-a-call –  Mar 19 '18 at 18:07
2

Concatenation in kotlin can be done in 3 ways

1 - Using String Templates

val myMessage = "${context.getString(R.string.mymessage)} ${it.myinfo.codeid}"

2 - Using + Sign

val myMessage = context.getString(R.string.mymessage) + " " + it.myinfo.codeid

3 - Using StringBuilder

val sb = StringBuilder()
val myMessage = sb.append(context.getString(R.string.mymessage)).append(it.myinfo.codeid)
Shivam Tripathi
  • 640
  • 8
  • 7
1

Either use + as known in Java:

context.getString(R.string.mymessage) + " " + it.myinfo.codeid

or use the more idiomatic templates approach:

"${context.getString(R.string.mymessage)} ${it.myinfo.codeid}"
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
  • any idea about this one : any idea about this one ? https://stackoverflow.com/questions/49302694/how-do-i-detect-skype-telegram-whatsapp-calls-when-my-messenger-app-is-in-a-call –  Mar 19 '18 at 18:19