Scala String can be defined as a sequence of characters. Today we will look into String concatenation, substring and some other Scala string functions.
Scala String
Consider an example of defining a string variable in Scala programming.
1 2 3 4 5 6 7 8 |
object Str { val st: String = "Scala is a functional programming language" def main(args:Array[String]) { println(st) } } |
Output: Scala is a functional programming language
Strings in Scala are same as java string and hence the value is of type java.lang.String
. Java classes are available in Scala, hence Scala makes use of java strings without creating a separate string class.
Similar to Java, String is immutable in Scala i.e. the object cannot be modified.
Creating String in Scala
Strings can be created by two ways as shown below.
1 2 3 4 |
val s1 = "String is a sequence of characters" val s2:String = "String is a sequence of characters" |
In the first case the compilers encounters a string literal and creates a string object s1.
In the second case, String type is specified before encountering the string literal.
If we need to append to the original string, StringBuilder
class is available in scala.
String Length in Scala
The methods used to obtain information about an object are known as accessor methods. The length()
method is one of the accessor methods which returns the number of characters in the string.
For example;
1 2 3 4 5 6 7 8 9 |
object strlen { def main(args: Array[String]) { var str = "Strings are immutable in scala"; var len = str.length(); println( "String Length is : " + len ); } } |
Output: String Length is : 30
Scala String Concatenation
Strings can be concatenated in two ways – one is using a concat
method and the other is using the +
operator.
Let us see an example of how to use concat method on strings.
1 2 3 4 5 6 7 8 9 10 |
object concat { def main(args: Array[String]) { var str1 = "String concatenation can be "; var str2 = "done using concat method"; var st = str1.concat(str2) println( "Concatenated String is : " + st ); } } |
Output: Concatenated String is : String concatenation can be done using concat method
The concat method accepts a string that is to be concatenated. Now let’s see an example of how to concatenate a string using + operator.
1 2 3 4 5 6 7 8 9 10 |
object strcon { def main(args: Array[String]) { var str1 = "Student name "; var str2 = "is Micheal"; var st = str1+ str2 println( "Concatenated String is : " + st ); } } |
Output: Concatenated String is : Student name is Micheal
Scala String Formatting
The printf()
and format()
methods are used to format the output numbers. The String class has format()
which returns a String object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
object format { def main(args: Array[String]) { var n1 = 78.99 var n2 = 49 var s1 = "Hello, World!" var f1 = printf("The value of the float variable is " + "%f, while the value of the integer " + "variable is %d, and the string " + "is %s", n1, n2, s1) println(f1) } } |
Output: The value of the float variable is 78.990000, while the value of the integer variable is 49, and the string is Hello, World!()
The format method can be used as below.
1 2 3 4 5 6 7 8 9 10 |
object formatmethod { def main(args:Array[String]){ val firstName = "Chris" val lastName = "Harris" val age = 12; println("%s %s, %d".format(firstName, lastName, age)); } } |
Output: Chris Harris, 12
Scala String Functions
Some of the string useful methods in Scala are;
- char charAt(int index) → Returns the character at the specified index.
- String replace(char c1, char c2) → Returns a new string resulting by replacing all occurrences of c1 in this string with c2.
- String[] split(String reg1) → Splits this string around matches of the given regular expression.
- String substring(int i1) → Returns a new string that is a substring of this string.
- String trim() → Returns a copy of the string, with leading and trailing whitespace omitted.
- String substring(int b1, int e1) → Returns a new string that is a substring of this string.
- boolean startsWith(String prefix) → Tests if this string starts with the specified prefix.
- boolean matches(String regex) → Tells whether or not this string matches the given regular expression.
- int hashCode() → Returns a hash code for this string.
That’s all for a quick roundup of String in Scala, we will look into more scala core features in coming posts.