F How to search and find Index Number of a word in a String in Java? | CodeTheta

How to search and find Index Number of a word in a String in Java?

April 04, 2018

Code : 
package demo;
import java.util.Scanner;
/**
 *
 * @author www.CodeTheta.com
 */
public class StringSearch {
    public static void main(String[] args) {
        String TextString;
        String FindString;
        System.out.println("Enter a string : ");
        Scanner in = new Scanner(System.in);
        TextString = in.next();
        System.out.println("Enter a word you want to find.");
        Scanner in1 = new Scanner(System.in);
        FindString = in1.next();
        int IndexNumber = TextString.indexOf(FindString);
        if(IndexNumber == - 1) {
            System.out.println("Not Found");
        } else {
            System.out.println("Entered Word Found at Index Number : " + IndexNumber);
        }
   }
}

Output :
Enter a string : 
codetheta
Enter a word you want to find.
d
Entered Word Found at Index Number : 2

IDE Used To Test This Code : NetBeans IDE 8.2

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Post a Comment