Strings
This document explains different string funcitons in scala. The below list will cover few popular functions.
S. No | Scala String Function | Description |
---|---|---|
1 | length | Returns the length of the string. |
2 | charAt(index: Int) | Returns the character at the specified index in the string. |
3 | substring(start: Int, end: Int) | Extracts a substring from the given start index to the end index (exclusive). |
4 | toUpperCase | Converts all characters in the string to uppercase. |
5 | toLowerCase | Converts all characters in the string to lowercase. |
6 | trim | Removes leading and trailing whitespaces from the string. |
7 | split(delimiter: String) | Splits the string into an array of substrings based on the given delimiter. |
8 | startsWith(prefix: String) | Checks if the string starts with the specified prefix. |
9 | endsWith(suffix: String) | Checks if the string ends with the specified suffix. |
10 | indexOf(str: String) | Returns the index of the first occurrence of the specified substring in the string. |
11 | lastIndexOf(str: String) | Returns the index of the last occurrence of the specified substring in the string. |
12 | replace(oldStr: String, newStr: String) | Replaces all occurrences of the old string with the new string. |
13 | isEmpty | Checks if the string is empty (contains no characters). |
14 | nonEmpty | Checks if the string is not empty (contains characters). |
15 | concat(str: String) | Concatenates the given string with the original string. |
16 | stripPrefix(prefix: String) | Removes the specified prefix from the beginning of the string. |
17 | stripSuffix(suffix: String) | Removes the specified suffix from the end of the string. |
18 | replaceAll(regex: String, replacement: String) | Replaces all substrings that match the given regular expression with the replacement string. |
19 | matches(regex: String) | Checks if the string matches the given regular expression. |
20 | charAt(index: Int) | Returns the character at the specified index in the string. |
21 | isEmpty | Checks if the string is empty (contains no characters). |
22 | nonEmpty | Checks if the string is not empty (contains characters). |
23 | distinct | Returns a new string with duplicate characters removed. |
24 | reverse | Reverses the order of characters in the string. |
25 | slice(start: Int, end: Int) | Extracts a slice of characters from the given start index to the end index (exclusive). |
26 | replaceAllLiterally(target: String, replacement: String) | Replaces all occurrences of the target string with the replacement string. This function performs a literal replacement without using regular expressions. |
27 | splitAt(index: Int) | Splits the string into two parts at the specified index. |
28 | contains(str: String) | Checks if the string contains the specified substring. |
29 | contentEquals(str: CharSequence) | Checks if the string's content is equal to the specified CharSequence. |
30 | matches(regex: Regex) | Checks if the string matches the specified regular expression (Regex). |
31 | compareTo(str: String) | Compares the string lexicographically with another string. |
32 | compareToIgnoreCase(str: String) | Compares the string lexicographically, ignoring case, with another string. |
33 | padTo(length: Int, elem: Char) | Pads the string with the specified character to the given length. |
34 | patch(start: Int, patch: String, replaced: Int) | Replaces 'replaced' characters at the 'start' index with the 'patch' string. |
35 | replaceFirst(regex: String, replacement: String) | Replaces the first occurrence of the regex with the replacement string. |
36 | replaceFirst(regex: Regex, replacement: String) | Replaces the first occurrence of the regex with the replacement string. |
37 | replaceSomeIn(target: CharSequence, replacer: String => Option[String]) | Replaces some occurrences of the target string based on a replacer function. |
38 | matchesPattern(str: String) | Checks if the string matches the pattern specified in a StringContext. |
39 | stripLineEnd | Removes any line ending characters (CR, LF) at the end of the string. |
40 | linesIterator | Returns an iterator of lines from the string. |
41 | format(args: Any*) | Creates a formatted string using the specified arguments. |
42 | getBytes(charset: String) | Converts the string to an array of bytes using the specified charset. |
43 | getChars(srcBegin: Int, srcEnd: Int, dest: Array[Char], destBegin: Int) | Copies characters from this string to the destination character array. |
44 | intern | Returns a canonical representation for the string. |
45 | codePointAt(index: Int) | Returns the Unicode code point at the specified index. |
46 | codePointBefore(index: Int) | Returns the Unicode code point before the specified index. |
47 | codePointCount(beginIndex: Int, endIndex: Int) | Returns the number of Unicode code points in the specified text range. |
48 | offsetByCodePoints(index: Int, codePointOffset: Int) | Returns the index within this string that is offset from the given index by codePointOffset code points. |
49 | matchesLiteral(str: String) | Checks if the string matches the specified literal. |
50 | getBytes | Encodes the string into a sequence of bytes using the platform's default charset. |
51 | getBytes(charset: Charset) | Encodes the string into a sequence of bytes using the specified charset. |
52 | stripMargin | Strips a margin from each line in the string. |
53 | stripMargin(marginChar: Char) | Strips a margin from each line in the string using the specified margin character. |
54 | stripMargin(margin: String) | Strips a margin from each line in the string using the specified margin string. |
55 | isBlank | Checks if the string is blank (empty or contains only whitespaces). |
56 | lines | Splits the string into a sequence of lines. |
57 | charAt(index: Int) | Returns the character at the specified index in the string. |
58 | getBytes | Encodes the string into a sequence of bytes using the platform's default charset. |
59 | getBytes(charset: Charset) | Encodes the string into a sequence of bytes using the specified charset. |
60 | equalsIgnoreCase(str: String) | Compares this string to another string, ignoring case considerations. |
61 | contentEquals(sb: StringBuffer) | Checks if the string's content is equal to the specified StringBuffer. |
62 | contentEquals(sb: StringBuilder) | Checks if the string's content is equal to the specified StringBuilder. |
63 | intern | Returns a canonical representation for the string. |
64 | linesIterator | Returns an iterator of lines from the string. |
65 | compareToIgnoreCase(str: String) | Compares the string lexicographically, ignoring case, with another string. |
String interpolators
In scala there are few string interpolators that provides more flexibility in concating strings.
1. S-interpolator
Below example explains how string contatination possible using dynamic variables in middle
val name = "Foo"
val welcomeMessage = s"Hello $name, welcome to the site." // the $name would be replaced with value and creates final string
val age = 10
val info = s"Your age is ${age + 3}" // This will also works.
2. F-interpolator
Below exmaple explains how it works
object InterpolatorsExample extends App {
val name = "Foo"
val zipCode = 500012
val salary = 25.323894f
val salrayMessage = f"Dear $name Your pay rate per hour is $salary%8.2f, your zip code is $zipCode%s"
println(salrayMessage)
}
3. Raw interpolator
The raw interpolator ignores escape characters inside, observe below example
println(s"1. Dear customer \n Welcome") // Prints new line like
1. Dear customer
Welcome
println(raw"2. Dear customer \n Welcome") // Prints raw characters
2. Dear customer \n Welcome