Web | This Site

Java Basics Examples
Java Tutorial
Java Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# Java Examples - Searching a Word

## Problem Description:

How to search a perticular word in a string?

## Solution:

Following example demonstrates how to search a perticular word in a string with the help of matcher.start() method of regex.Matcher class.

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class Main {
       public static void main(String args[]) {
          Pattern p = Pattern.compile("j(ava)");
          String candidateString = "This is a java program.
          This is another java program.";
          Matcher matcher = p.matcher(candidateString);
          int nextIndex = matcher.start(1);
          System.out.println(candidateString);
          System.out.println("The index for java is:"
          + nextIndex);
       }
    }

## Result:

The above code sample will produce the following result.

    This is a java program. This is another java program.
    The index for java is: 11

Advertisement

Online Image Processing

Indian Baby Names