Scala String is a sequence of characters. It is immutable object which means it cannot be changed once created. In this blog we will look at all the String Functions that is available in Scala. As there are many string functions I will be covering them over a period of time, so initially the list of functions could be small but gradually I will list down everything in this blog.
Table of Contents
Scala charAt() method with example
charAt takes an integer and returns the character present at the index of the string.
(arg0: Int): Char
Scala codePointAt() method with example
codePointAt takes integer and returns a non-negative integer that is the UTF-16 code point value
Scala compareTo() method with example
compareTo compares the given string with the current string lexicographically. If the first string is lexicographically greater than the second string, it returns a positive number (difference of character value). If the first string is less than the second string lexicographically, it returns a negative number, and if the first string is lexicographically equal to the second string, it returns 0.
compareToIgnoreCase is similar to compareTo with the difference being it assumes capital letter to be same as small letter.
Scala concat() method with example
concat creates a new string by concatenating 2 strings. We can also use + operator for the same.
Scala contains() method with example
contains tests whether the substring is present in the given string. Returns: If the substring is found in the specified String, it returns a true value ,otherwise, it returns a false value.
Scala equals() method with example
With this method you will be able to compare 2 strings. Which means it will check if it has exact same sequence of characters. If successful it returns true else false.
One thing to notice is the equals() method also check if both are of string datatypes. If one of the datatype is different then it returns false.
val str3: StringBuffer = new StringBuffer("subway") str1.equals(str3) //false
equalsIgnoreCase() is similar to equals() methods with the difference being it assumes capital letter to be same as small letter.
Scala contentEquals() method with example
Using contentEquals() method you can compare String value with StringBuffer and also String with another String. If successful it returns true else false.
Scala endsWith() method with example
endsWith() method is used to check whether the string ends with the given suffix substring passed as parameter to the method. If successful it returns true else false.
Scala getBytes() method with example
getBytes() method is used to convert the string to an array of bytes. The elements of the byte array are the ascii values of the character of the string and these values are stored in a byte array. This method does not take any parameter .