Home »
Scala
Scala - String Methods
By IncludeHelp Last updated : October 20, 2024
Character Access and Manipulation Methods
The following table lists the methods for accessing and manipulating characters in strings:
Method |
Description |
charAt() |
Returns the character from the string at a given index. |
length() |
Return the length of the given string. It returns an integer value which is the number of characters in the string. |
toCharArray() |
Converts the given string to a character array (charArray). |
Comparison Methods
The following table lists the methods for string comparison:
Method |
Description |
compareTo() |
Compares two strings and return the integer value denoting their difference. |
compareToIgnoreCase() |
Compare two strings in Scala by ignoring case differences. |
contentEquals() |
Compares the contents of a string and a string buffer. It checks for the equality of both. |
equals() |
Compares the given strings and return a Boolean value based on the comparison. The strings can be string or any other object. |
equalsIgnoreCase() |
Compares string and object. The method ignores cases of characters of the string while making the comparison. |
regionMatches() |
Checks if the given region (substring) is common between the two strings or not. |
Substring and Subsequence Methods
The following table lists the methods for finding substring and subsequence:
Method |
Description |
substring() |
Create a substring from the string from the startingIndex to endingIndex. |
Subsequence() |
Create a sub-sequence from the string from the startingIndex to endingIndex. |
Searching Methods
The following table lists the methods for searching in a string:
Method |
Description |
lastIndexof() |
Returns the index of the last occurrence of the sub-string which is passed as parameter to the method in the calling string. |
startsWith() |
Checks whether the calling string starts with the string inside the parameter. |
endsWith() |
Checks whether the string ends with the given suffix substring passed as parameter to the method. |
matches() |
Check whether the regular expression passed as parameter is present in the string or not. |
String Replacement Methods
The following table lists the methods for string Replacement:
Method |
Description |
replace() |
Replaces a character from the given string with a new character. |
replaceFirst() |
Replace the first occurrence of the given substring with another given string. |
replaceAll() |
Replaces all occurrences of the given substring with another string. |
String Transformation Methods
The following table lists the methods for string transformation:
Method |
Description |
toUpperCase() |
Converts the string's character to uppercase characters. |
toLowerCase() |
Converts the string's character to lowercase characters. |
trim() |
Removes extra space from the starting and ending of the string. |
Utility Methods
The following table lists the other utility nethods:
Method |
Description |
getBytes() |
Converts the given string into an array of bytes. |
hashCode() |
Returns the hash code for the given string in Scala. |
intern() |
Checks canonical presentation of the given string. It returns the standard representation of the given string. |
split() |
Splits the given string into an array of strings using the separator passed as parameter. |