1.

How can you concatenate two strings in Kotlin?

Answer»

Following are the different ways by which we can concatenate two STRINGS in Kotlin:

Using String Interpolation:- We use the technique of string interpolation to concatenate the two strings. Basically, we SUBSTITUTE the strings in place of their placeholders in the initialisation of the third string.

VAL s1 = "Interview"val s2 = "Bit"val s3 = "$s1 $s2" // stores "Interview Bit"

Using the + or plus() operator:- We use the ‘+’ operator to concatenate the two strings and store them in a third variable.

val s1 = "Interview"val s2 = "Bit"val s3 = s1 + s2 // stores "InterviewBit"val s4 = s1.plus(s2) // stores "InterviewBit"

Using STRINGBUILDER:- We concatenate two strings using the StringBuilder object. First, we append the first string and then the second string. 

val s1 = "Interview"val s2 = "Bit"val s3 =  StringBuilder()    s3.append(s1).append(s2)val s4 = s3.toString() // stores "InterviewBit"


Discussion

No Comment Found