

This feature supports the versions of Scala-2.10 and later. String Interpolation is the new way to create Strings in Scala programming language. While the value of the integer variable is 2000, The value of the float variable is 12.456000, The following commands are used to compile and execute this program. Var fs = printf("The value of the float variable is " + "%f, while the value of the integer " + "variable is %d, and the string" + "is %s", floatVar, intVar, stringVar) Try the following example program, which makes use of printf() method − Example The String class has an equivalent class method, format(), that returns a String object rather than a PrintStream object. You have printf() and format() methods to print output with formatted numbers.

#Scala convert string to long code#
The following lines of code to find string length. Strings are more commonly concatenated with the + operator, as in − You can also use the concat() method with string literals, as in − This returns a new string that is string1 with string2 added to it at the end. The String class includes a method for concatenating two strings − Use the following code segment to find the length of a string − Example One accessor method that can be used with strings is the length() method, which returns the number of characters contained in the string object. Methods used to obtain information about an object are known as accessor methods. If there is a necessity to make a lot of modifications to Strings of characters then use String Builder Class available in Scala!. String object once created cannot be changed. CommandĪs mentioned earlier, String class is immutable. String keyword can also be given in alternate declaration as shown above. Whenever compiler encounters a string literal in the code, it creates a String object with its value, in this case, “Hello world!”. The following code can be used to create a String −

Strings are very useful objects, in the rest of this section, we present important methods of class. On the other hand, objects that can be modified, like arrays, are called mutable objects. In Scala, as in Java, a string is an immutable object, that is, an object that cannot be modified. This chapter takes you through the Scala Strings.
