1. Comparing strings
One of the most common operations with strings is comparison. The String class has over ten different methods that are used to compare one string with another string. Below we will take a look at seven of the main ones.
Methods | Description |
---|---|
|
Strings are considered equal if all of their characters match. |
|
Compares strings, ignoring the case of letters (ignores whether they are uppercase or lowercase) |
|
Compares strings lexicographically. Returns 0 if the strings are equal. The return value is less than zero if the current string is less than the string parameter. The return value is greater than if the current string is greater than the string parameter. |
|
Compares strings lexicographically while ignoring case. Returns 0 if the strings are equal. The return value is negative if the current string is less than the string parameter. The return value is greater than if the current string is greater than the string parameter. |
|
Compares parts of strings |
|
Checks whether the current string starts with the string prefix |
|
Checks whether the current string ends with the string suffix |
Let's say you want to write a program that asks the user for a path to a file and then checks the file type based on its extension. The code of such a program might look something like this:
Code | Notes |
---|---|
|
Create a Scanner objectRead a line from the console Check whether the string path ends with the given string
|
2. Searching for substrings
After comparing strings, the second most popular operation is finding one string inside another. The String class also has a few methods for this:
Methods | Description |
---|---|
|
Searches for the string str in the current string. Returns the index of the first character of the first occurrence. |
|
Searches for the string str in the current string, skipping the first index characters. Returns the index of the occurrence. |
|
Searches for the string str in the current string, starting from the end. Returns the index of the first occurrence. |
|
Searches for the string str in the current string from the end, skipping the first index characters. |
|
Checks whether the current string matches a pattern specified by a regular expression. |
The indexOf(String)
and indexOf(String, index)
methods are often used in combination. The first method lets you find the first occurrence of the passed substring in the current string. And the second method lets you find the second, third, etc. occurrences by skipping the first index characters.
Suppose we have a url like "https://domain.com/about/reviews", and we want to replace the domain name with "codegym.cc". Urls can have all sorts of different domain names, but we know the following:
- The domain name is preceded by two forward slashes — "
//
" - The domain name is followed by a single forward slash — "
/
"
Here's what the code for such a program would look like:
Code | Notes |
---|---|
|
Create a Scanner object Read a line from the console Get the index of the first occurrence of the string " // "We get the index of the first occurrence of the string / , but look only after an occurrence of the characters // .We get the string from the beginning to the end of the characters // We get the string from / to the end.We concatenate the strings and the new domain. |
The lastIndexOf(String)
and lastIndexOf(String, index)
methods work the same way, only the search is performed from the end of the string to the beginning.
3. Creating substrings
In addition to comparing strings and finding substrings, there is another very popular action: getting a substring from a string. As it happens, the previous example showed you a substring()
method call that returned part of a string.
Here is a list of 8 methods that return substrings from the current string:
Methods | Description |
---|---|
|
Returns the substring specified by the index range beginIndex..endIndex . |
|
Repeats the current string n times |
|
Returns a new string: replaces the character oldChar with the character newChar |
|
Replaces the first substring, specified by a regular expression, in the current string. |
|
Replaces all substrings in the current string that match the regular expression. |
|
Converts the string to lowercase |
|
Converts the string to uppercase |
|
Removes all spaces at the beginning and end of a string |
Here is a summary of the available methods:
substring(int beginIndex, int endIndex)
method
The substring
method returns a new string that consists of characters in the current string, starting at the character with index beginIndex
and ending at endIndex
. As with all intervals in Java, the character with index endIndex
is not included in the interval. Examples:
Code | Result |
---|---|
|
|
|
|
|
|
|
|
If the endIndex
parameter is not specified (which is possible), then the substring is taken from character at beginIndex to the end of the string.
repeat(int n)
method
The repeat method simply repeats the current string n
times. Example:
Code | Result |
---|---|
|
|
|
|
|
|
|
|
replace(char oldChar, char newChar)
method
The replace()
method returns a new string in which all characters oldChar
are replaced with the character newChar
. This does not change the length of the string. Example:
Code | Result |
---|---|
|
|
|
|
|
|
|
|
replaceFirst()
and replaceAll()
methods
The replaceAll()
method replaces all occurrences of one substring with another. The replaceFirst()
method replaces the first occurrence of the passed substring with the specified substring. The string to be replaced is specified by a regular expression. We will delve into regular expressions in the Java Multithreading quest.
Examples:
Code | Result |
---|---|
|
|
|
|
|
|
|
|
toLowerCase() and toUpperCase()
methods
We got to know these methods when we first learned about calling the methods of the String
class.
trim()
method
The trim()
method removes leading and trailing spaces from a string. Does not touch spaces that are inside a string (i.e. not at the beginning or end). Examples:
Code | Result |
---|---|
|
|
|
|
|
|
|
|
GO TO FULL VERSION