Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, the String
type represents a sequence of characters. Scala doesn't have its own native String
class; instead, it uses the Java String
class from java.lang
package, which means that you can use Java's String
methods directly in Scala. However, due to Scala's rich standard library, you often have more concise and functional ways to manipulate and process strings.
Here are some fundamental aspects of String
in Scala:
Creation:
val str = "Hello, World!"
Concatenation:
val greeting = "Hello" val name = "Alice" val message = greeting + ", " + name + "!"
String Interpolation:
s-interpolator:
val age = 30 val interpolated = s"My age is $age"
f-interpolator (similar to printf in other languages):
val height = 1.9 val formatted = f"I am $height%2.2f meters tall"
raw-interpolator:
val rawString = raw"This is a \n raw string" println(rawString) // Prints: This is a \n raw string
Multiline Strings:
val multiline = """This is a multiline string"""
Common Operations:
val s = "hello" s.length // 5 s.charAt(1) // 'e' s.substring(1, 3) // "el" s.contains("ell") // true s.split(" ") // Array of words s.toLowerCase() // "hello" s.toUpperCase() // "HELLO"
Rich Operations with StringOps
: Implicitly, when you're using a String
in Scala, you can also leverage methods provided by StringOps
due to implicit conversions. This class provides many functional methods like map
, filter
, foreach
, etc.
"hello".reverse // "olleh" "hello".drop(2) // "llo"
Comparisons:
val s1 = "hello" val s2 = "Hello" s1 == s2 // false s1.equalsIgnoreCase(s2) // true
Regular Expressions: Scala offers support for regex matching and extraction.
val pattern = "(H|h)ello".r val result = pattern.findFirstIn("Hello, World!")
Remember, since Scala String
is the same as Java String
, you have access to all the methods available in the Java String API. Plus, with the added power of Scala's library enhancements and functional programming features, you can achieve more concise and expressive string manipulations.
String Manipulation in Scala:
Perform basic string operations like concatenation, length, and accessing characters.
val str1 = "Hello" val str2 = "Scala" val concatenated = str1 + " " + str2
Scala String Interpolation:
Use string interpolation for easy incorporation of variables into strings.
val name = "John" val age = 30 val message = s"My name is $name and I'm $age years old."
Working with Scala Strings:
Utilize various String methods for manipulation.
val text = "Scala is powerful" val upperCase = text.toUpperCase val reversed = text.reverse
Concatenating Strings in Scala:
Combine strings using concatenation or the concat
method.
val str1 = "Hello" val str2 = "World" val result = str1.concat(" ").concat(str2)
String Formatting in Scala:
Format strings using printf
or the format
method.
val pi = 3.14159 val formatted = f"Value of pi: $pi%.2f"
Scala Multiline Strings:
Create multiline strings using triple double-quotes.
val multilineString = """This is a |multiline |Scala string."""
Pattern Matching with Scala Strings:
Use pattern matching to match specific patterns in strings.
val input = "123" val result = input match { case "123" => "Match found" case _ => "No match" }
Substring Operations in Scala:
Extract substrings using substring
or string range operations.
val text = "Scala is fun" val substring1 = text.substring(6, 8) val substring2 = text.slice(6, 8)