Kotlin packs great features with commonly used data type String. One of the really cool features is String templates. This feature allows Strings to contain template expression.
In Java, you had to use StrSubstitutor (https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StrSubstitutor.html) and a map to go with it. A template expression in Java will look as follows:
Map<String, String> valuesMap = new HashMap<String, String>();
valuesMap.put("city", "Paris");
valuesMap.put("monument", "Eiffel Tower");
String templateString ="Enjoyed ${monument} in ${city}.";
StrSubstitutorsub=newStrSubstitutor(valuesMap);
String resolvedString =sub.replace(templateString);
Kotlin eases out the pain in writing template expressions and makes it fun, concise, and a lot less verbose.
Using String templates, you can embed a variable or expression inside a string without string concatenation. So, let’s get started!