Scala String concatenation, substring, length functions With Examples

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.

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.

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;

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.

Output: Concatenated String is : String concatenation can be done using concat method

Scala-String-length

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.

Output: Concatenated String is : Student name is Micheal

Scala-String-length

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.

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-length

The format method can be used as below.

Output: Chris Harris, 12

Scala-String-length

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.

By admin

Leave a Reply

%d bloggers like this: