A method to get an arbitrary subsequence from a string. For example, from the string "It's nice weather today", just the string "Weather" Can be extracted.
String class methods java.lang.String.substring()
substring: Substring, substring, substring
public static void main(String[] args) {
//Character string before extraction
String str = "abcde";
//Display the extracted character string
System.out.println(str.substring(2,4));
}
Execution result
cd
To specify the character string to be extracted, draw a line between the characters as shown below. It is easy to understand if you number it.
|a|b|c|d|e| 0 1 2 3 4 5
|now|Day|Is|I|I|Heaven|Qi|so|Su|Ne| 0 1 2 3 4 5 6 7 8 9 10
If you specify only the start position and not the end position, the remaining sedge characters are extracted.
public static void main(String[] args) {
//Character string before extraction
String str = "The weather is good today, is not it";
//Display the extracted character string
System.out.println(str.substring(5));
}
Execution result
It's the weather
Recommended Posts