Write a memo as a method of acquiring a character string.
In the String class, a specific character string can be obtained by using the indexOf method to get the first appearing character string and the lastIndexOf method to get the last appearing character string.
The basic writing style is like this.
First occurrence position = object .indexOf (value)
Last occurrence position = object .lastIndexOf (value)
This time I will summarize how to use it in the String class.
⑴ indexOf method
・ IndexOf (int ch) (Return type is int)
Returns the index of the position where the specified character first appears in this string. If the character with the value ch is in the character sequence represented by this String object, the index (in Unicode code units) of the first occurrence position is returned. If the value of ch is in the range 0 to 0xFFFF, the minimum value k is returned so that the following expression is true. this.charAt(k) == ch Is true. If ch is any other value, then the minimum value k such that the following expression is true. this.codePointAt(k) == ch Is true. If no such character is in this string, -1 is returned. Parameters: ch-Character (Unicode code point). Return value: The index of the position where the specified character first appears in the character sequence represented by this object. -1 if there are no characters.
・ IndexOf (int ch, int from Index) (Return type is int) Within this string, the search starts at the specified index and returns the index where the specified character first appears. If the character with the value ch is greater than or at the same index position as the fromIndex of the character sequence represented by this String object, the corresponding first index is returned. If the value of ch is in the range 0 to 0xFFFF, the minimum value k is returned so that the following expression is true. (this.charAt(k) == ch) && (k >= fromIndex)
Is true. If ch is any other value, then the minimum value k such that the following expression is true. (this.codePointAt(k) == ch) && (k >= fromIndex)
Is true. In either case, -1 is returned if no such character exists at or after the position fromIndex in this string. There are no restrictions on the value of fromIndex. Negative values have the same effect as zero. The entire string is searched. If it is greater than the length of this string, the result is the same as if it is equal to the length of this string, and -1 is returned. All indexes are specified in char values (in Unicode code units). Parameters: ch-Character (Unicode code point). fromIndex-The index of the search start position. Return value: In the string represented by this object, the index of the first occurrence position if the specified character is at an index position equal to or greater than fromIndex. -1 if there are no characters.
(2) lastIndexOf method ・ LastIndexOf (int ch) Returns the index of the position where the specified character last appears in this string. If the ch value is in the range 0 to 0xFFFF, the index returned (in Unicode code units) is the maximum value k for the following expression: this.charAt(k) == ch Is true. If ch is any other value, then the maximum value k such that the following expression is true. this.codePointAt(k) == ch Is true. If no such character is in this string, -1 is returned. The search for String starts at the last character and goes to the beginning. Parameters: ch-Character (Unicode code point). Return value: The index of the position where the specified character last appears in the character sequence represented by this object. -1 if there are no characters.
・ LastIndexOf (int ch, int fromIndex) (Return type is int)
Returns the index of the position where the specified character last appears in this string (the search starts at the specified index and goes first). If the ch value is in the range 0 to 0xFFFF, the index returned is the maximum value k for the following expression: (this.charAt(k) == ch) && (k <= fromIndex) Is true. If ch is any other value, then the maximum value k such that the following expression is true. (this.codePointAt(k) == ch) && (k <= fromIndex) Is true. In either case, -1 is returned if no such character exists at or before the position fromIndex in this string. All indexes are specified in char values (in Unicode code units). Parameters: ch-Character (Unicode code point). fromIndex-The index of the search start position. There are no restrictions on the value of fromIndex. If it is equal to or greater than the length of this string, the result is the same as if it is one less than the length of this string, and the entire string is searched. Negative values give the same result as -1 and return -1. Return value: The index of the position where the specified character last appears at an index position equal to or less than fromIndex in the character sequence represented by this object. -1 if the specified character does not precede its position.
Reference site -Java (tm) Platform Standard Edition 8 Class String https://docs.oracle.com/javase/jp/8/docs/api/index.html?overview-summary.html
・ Ready to use! Java indexOf and lastIndexOf-Engineer's entrance https://eng-entrance.com/java-indexof
Recommended Posts