Scala String concatenation, substring, length functions

Filed Under: Scala

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.


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.


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;


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 length

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.


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

Scala String Concatenation

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.


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 Concat + operator

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.


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!()

Scala String Format

The format method can be used as below.


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

Scala String Functions

Some of the string useful methods in Scala are;

  1. char charAt(int index) → Returns the character at the specified index.
  2. String replace(char c1, char c2) → Returns a new string resulting by replacing all occurrences of c1 in this string with c2.
  3. String[] split(String reg1) → Splits this string around matches of the given regular expression.
  4. String substring(int i1) → Returns a new string that is a substring of this string.
  5. String trim() → Returns a copy of the string, with leading and trailing whitespace omitted.
  6. String substring(int b1, int e1) → Returns a new string that is a substring of this string.
  7. boolean startsWith(String prefix) → Tests if this string starts with the specified prefix.
  8. boolean matches(String regex) → Tells whether or not this string matches the given regular expression.
  9. 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.

Comments

  1. thangamani says:

    plz give example of scala standard library methods

  2. Manjunath says:

    could you let me know how do perform operation of convert a stream of strings to integer and perform summation

  3. anusuya says:

    good.very useful for beginners in scala.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages