Summary of search methods for substrings in Java strings

There are four methods to find the substring of a string in Java, as follows: 1. Int indexof (string STR): returns the index of the specified substring in this string for the first time. 2. Int indexof (string STR, int StartIndex): starting from the specified index, returns the index of the specified substring in this string for the first time. 3. Int lastIndexOf (string STR): returns the index of the specified substring appearing on the rightmost side of this string. 4. Int lastIndexOf (string STR, int StartIndex): search backward from the specified index and return the index of the specified substring that last appears in this string.

Usage description of indexof()

Returns the character position of the first occurrence of a substring in a string object.

parameter

Required. String object or text.

Substring required.

The substring to find in the string object.

Starindex optional.

This integer value indicates the index to start the lookup within the string object. If omitted, look at the beginning of the string.

Description the indexof method returns an integer value indicating the starting position of the substring in the string object. If no substring is found, - 1 is returned.

If StartIndex is negative, StartIndex is treated as zero. If it is larger than the largest character position index, it is regarded as the largest possible index.

Perform a lookup from left to right. Otherwise, the method is the same as lastIndexOf.

Example the following example illustrates the use of the indexof method.

Java finds the number of substrings contained in the string 1 Using indexof:

2. If the substring is not a string with the same beginning and end, it can also be implemented as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>