Use Java's String.indexOf() function to find the position of a character (string) in a string
In Java, we often need to find the position of a character or substring in a string. . In order to achieve this function, Java provides the indexOf() function in the String class. This function can help us quickly determine the position of the target character (string) in the string.
String.indexOf() function can accept two parameters, namely the target character (string) to be found and the starting position. It searches the string starting from the starting position and returns the index position of the target character (string) in the string. If the target character (string) cannot be found, -1 is returned.
The following is a simple example that demonstrates how to use the String.indexOf() function to find the position of a character in a string:
public class StringIndexOfExample { public static void main(String[] args) { String str = "Hello, world!"; char target = 'o'; int position = str.indexOf(target); if (position != -1) { System.out.println("目标字符 '" + target + "' 在字符串中的位置为: " + position); } else { System.out.println("目标字符 '" + target + "' 不存在于字符串中"); } } }
In the above example, we define a character The string str
and a target character target
. Then, we use the String.indexOf() function to find the position of the target character in the string and save the result in the position
variable.
Finally, we determine whether the target character is in the string by judging whether the value of position
is -1. If it is not -1, it means that the target character exists in the string, and the position of the target character is output; if it is -1, it means that the target character does not exist in the string.
In addition to finding the position of a single character, we can also use the String.indexOf() function to find the position of a substring in a string. Here is an example that demonstrates how to find the position of a substring within a string:
public class StringIndexOfExample { public static void main(String[] args) { String str = "Hello, world!"; String target = "world"; int position = str.indexOf(target); if (position != -1) { System.out.println("目标子字符串 '" + target + "' 在字符串中的位置为: " + position); } else { System.out.println("目标子字符串 '" + target + "' 不存在于字符串中"); } } }
In the above example, we defined a string str
and a target subcharacter stringtarget
. We then use the String.indexOf() function to find the position of the target substring in the string and save the result in the position
variable.
Finally, we determine whether the target substring is in the string by judging whether the value of position
is -1. If it is not -1, it means that the target substring exists in the string, and the position of the target substring is output; if it is -1, it means that the target substring does not exist in the string.
To sum up, by using Java's String.indexOf() function, we can easily find the position of the target character or substring in the string. This function is one of the common tools for Java string processing, which can greatly improve the efficiency and convenience of our string operations. I hope this article will help you understand and use the String.indexOf() function!
The above is the detailed content of Use java's String.indexOf() function to find the position of a character (string) in a string. For more information, please follow other related articles on the PHP Chinese website!