Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - How do I concatenate a String to an Int value?

Tags:

kotlin

A very basic question, What is the right approach to concatenate String to an Int?

I'm new in Kotlin and want to print an Integer value preceding with String and getting the following error message.

for (i in 15 downTo 10){
  print(i + " "); //error: None of the following function can be called with the argument supplied:
  print(i); //It's Working but I need some space after the integer value.
}

Expected Outcome
15 14 13 12 11 10

like image 741
Nassa44 Avatar asked Sep 20 '25 21:09

Nassa44


1 Answers

You've got several options:

1. String templates. I think it is the best one. It works absolutely like 2-st solution, but looks better and allow to add some needed characters.

print("$i")

and if you want to add something

print("$i ")
print("$i - is good")

to add some expression place it in brackets

print("${i + 1} - is better")

2. toString method which can be used for any object in kotlin.

print(i.toString())

3. Java-like solution with concatenating

print("" + i)
like image 98
Ircover Avatar answered Sep 22 '25 18:09

Ircover