How to Find the Position of a Character, Word, or Phrase in a String in Java

Java





In this article, we show how to find the position of a character, word, or phrase in Java.

We'll start with finding the position of a character in Java.

This is very important and can be utilized for many reasons. Imagine if you are uploading files with Java and you want to know what type of file it is. In this case, you would look for the dot or period in the string. Then what follows this is the file extension of the file.

Another example would be domain names for websites. You would look for the dot or period. What follows are this is the type of domain it is, whether it is com, edu, org, xyz, etc.

In Java, the position of a string can be found by the indexOf() method.

The program below finds the position of the period in the string below.



So this code above is a public class named TypeofFile.

After the main method, we declare a string named filename. This string is set equal to "helloworld.php".

Next, we declare an integer named position. This position variable is set equal to filename.indexOf("."). The indexOf() method finds the position of the period (".").

The following line then outputs the position to the console.

When taking into account the indexOf() method, the first character in a string is 0 (not 1). So if you have a string, "Rob", R is at position 0. If you use the indexOf() method to find the position of 'R' in "Rob", the method will return 0. "o" is at position 1. And "b" is at position 2.

Many other languages use this orientation with the starting point at position 0, so it's nothing new.

So the indexOf() method can be used to find the position of a character in a string. In the same way that it is used to find a character in a string, it can be used to find a word or phrase in a string. You just replace the string, as shown above, with a character or phrase. It then finds where that word or phrase begins in the string. So, for example, if you are looking for Smith in the string, "John Smith", the indexOf() method will return 6, since "Smith" begins at position 6.

So it's really the same thing. Instead of finding what position the character is at, you're finding what position that word or phrase begins at.

IndexOf() Method to See if Character or Phrase Exists in String

The indexOf() method also has other uses. It can be used to find out whether a character, word, or phrase exists in a string. If it does, you will get a number that is either 0 or a positive number. If it is not present in a string, -1 will be returned by the indexOf() method.

So you can use a simple if statement that if the number returned by the indexOf() method is equal to or greater than 0, that character (word or phrase) is present in the string. If not, it does not exist in the string.

The program below sees if the word, "candy" exists in the string.



So the code above finds the position of candy using the indexOf() method and stores it into the integer position.

If the value stored in the variable position is equal to or greater than 1, this means that the word we are searching for, in this case "candy", exists in the string.

If the value stored in the variable position is not equal to or greater than 0, this means that the word we are searching for does not exist in the string.

So keep in mind that you can also use the indexOf() method to find whether a character, word, or phrase exists in a string.


Related Resources

How to Read Input with the JOptionPane Class in Java

HTML Comment Box is loading comments...