I am studying for the Java SE 8 Silver exam, and I would like to summarize what I learned during that time.
This time, I will organize various methods of the String class.
(I regret that there are many articles on character string manipulation ... sweat)
I have summarized the main methods that appear in the exam.
Method name | Description |
---|---|
char charAt(int i) | Returns the character at position i. |
String concat(String str) | Add a string |
boolean endsWith(String suffix) | Returns true if the string ends with the specified string, false if not. |
boolean equalsIgnoreCase(String str) | Compare strings. Insensitive to case. |
int indexOf(int ch) | Returns the position where the value entered in the actual argument first appears. |
int lastIndexOf(int ch) | Returns the position where the value entered in the actual argument appears last. |
int length() | Returns the number of characters in the string. |
String replace(char o, char n) | Returns the result of replacing the character o in the string with the character n. |
boolean startsWith(String prefix) | Returns true if the string starts with the value you put in the actual argument. |
String substring(int i) | 0 ~Returns a substring up to the value entered in the actual argument. (You can specify the start point.) |
String toLowerCase() | Convert uppercase to lowercase |
String toUpperCase() | Convert lowercase letters to uppercase |
String toString() | Returns the string held by the object. |
String trim() | Remove whitespace. (However, full-width space symbols are not removed) |
I'll delve into the methods that I find difficult to understand.
-Substring () method
Regarding the method of extracting the character string, if you do not pay attention to how to specify the range, the output will not be as expected.
Below is the sample code.
Main.java
public class Main {
public static void main(String[] args) {
String str = "There was a pig kimchi";
//Explanation ①
System.out.println(str.substring(0, 5));
}
}
Output result
Main.java
Pig kimchi
Analysis ① Only "Pig Kimchi" is taken out, but when taking out ***, there must be 0 before the first character ***
Please be careful. It can be taken out by showing the range from 0 to 5.
Buta Kimchi is Ah, Ma.
↑ ↑ ↑ ↑ ↑ ↑ . . . .
0 1 2 3 4 5
Another thing I want to organize is the trim () method.
・ Trim () method
It is a method that removes spaces before and after the character string.
It also removes tab characters such as \ t, \ n or \ r, and newlines.
Please note that it does not remove double-byte spaces and does not remove whitespace in the string.
There are many problems that can be understood by solving Java Silver problems.
Even though I called a convenient method of the String class and fetched the value
Since it is not assigned, there are many nasty problems such as the output is the same as before calling the method.
Therefore, I would like to take on the challenge with plenty of time when taking the exam.
Recommended Posts