[Java] How to get to the front of a specific string using the String class

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

[Java] How to get to the front of a specific string using the String class
[Java] How to easily get the longest character string of ArrayList using stream
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to check for the contents of a java fixed-length string
[Java] How to get the authority of the folder
How to find out the Java version of a compiled class file
How to get the absolute path of a directory running in Java
[Java] How to get the URL of the transition source
[Java] How to get the maximum value of HashMap
[Java] How to erase a specific character from a character string
How to get the class name / method name running in Java
[jsoup] How to get the full amount of a document
[Java] How to use the File class
[Java] Get the day of the specific day of the week
[Java] How to use the HashMap class
[Java] How to get the current directory
[Processing × Java] How to use the class
How to get the date in java
[Java] How to use the Calendar class
How to get the contents of Map using for statement Memorandum
[Java] How to convert from String to Path type and get the path
How to get the length of an audio file in java
[Java] Get the length of the surrogate pair string
Set the time of LocalDateTime to a specific time
How to get Class from Element in Java
How to get today's day of the week
How to convert a solidity contract to a Java contract class
How to get the current date as a string in yyyyMMdd format
[Java] How to use substring to cut out a part of a character string
[Java] How to get random numbers excluding specific numbers
[Java] How to get the redirected final URL
How to get the query string to actually issue when using PreparedStatement with JDBC
[Java] How to convert one element of a String type array to an Int type
How to get the ID of a user authenticated with Firebase in Swift
I want to recursively get the superclass and interface of a certain class
[Java] How to use compareTo method of Date class
How to write Scala from the perspective of Java
As of April 2018 How to get Java 8 on Mac
[Java] How to cut out a character string character by character
[Android] How to get the setting language of the terminal
How to convert A to a and a to A using AND and OR in Java
[Rails] How to get the contents of strong parameters
[Swift] How to get the document ID of Firebase
How to use java class
<Java> Quiz to batch convert file names separated by a specific character string with a part of the file name
A memo about the types of Java O/R mappers and how to select them
An unsupported Java version How to get rid of errors
Get to the abbreviations from 5 examples of iterating Java lists
[Java] How to convert a character string from String type to byte type
How to store a string from ArrayList to String in Java (Personal)
How to get the longest information from Twitter as of 12/12/2016
[Java] Try editing the elements of the Json string using the library
How to move another class with a button action of another class.
[Java] How to use substring to cut out a character string
[Ruby] How to retrieve the contents of a double hash
How to mock some methods of the class under test
How to derive the last day of the month in Java
The story of pushing Java to Heroku using the BitBucket pipeline
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
How to play MIDI files using the Java Sound API
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.