top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is String Interpolation in Scala? How do we use String Interpolation in Scala?

+3 votes
343 views
What is String Interpolation in Scala? How do we use String Interpolation in Scala?
posted Jun 28, 2016 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

Hi,String Interpolation means replacement of defined variables or expressions in the string with values. We should use s String Interpolator to define processed String Literal as shown below:
Example:-

scala> val definedVariableOne = "Questions"
definedVariables: String = Questions
scala>val definedVariableTwo = "QueryHome"
definedVariableTwo: String =For QueryHome
scala>val siteOne = "Site($definedVariableOne For for $definedVariableTwo)"
detailOne: String =Site($definedVariableOne For for $definedVariableTwo)
scala>val detailTwo = s"Site($definedVariableOne For for $definedVariableTwo)"
detailTwo: String = Site(Questions For QueryHome)

If we observe both detailOne and detailTwo results, we can observe that in detailTwo we have used String Interpolation concept that’s why that String is evaluated to correct expected result.

answer Jun 28, 2016 by Shivam Kumar Pandey
...