
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to parse for words in a string for a specific word in java?
There are various methods in Java using which you can parse for words in a string for a specific word. Here we are going to discuss 3 of them.
The contains() method
The contains() method of the String class accepts a sequence of characters value and verifies whether it exists in the current String. If found it returns true else, it returns false.
Example
import java.util.StringTokenizer; import java.util.regex.Pattern; public class ParsingForSpecificWord { public static void main(String args[]) { String str1 = "Hello how are you, welcome to Tutorialspoint"; String str2 = "Tutorialspoint"; if (str1.contains(str2)){ System.out.println("Search successful"); } else { System.out.println("Search not successful"); } } }
Output
Search successful
The indexOf() method
The indexOf() method of the String class accepts a string value and finds the (starting) index of it in the current String and returns it. This method returns -1 if it doesn’t find the given string in the current one.
Example
public class ParsingForSpecificWord { public static void main(String args[]) { String str1 = "Hello how are you, welcome to Tutorialspoint"; String str2 = "Tutorialspoint"; int index = str1.indexOf(str2); if (index>0){ System.out.println("Search successful"); System.out.println("Index of the word is: "+index); } else { System.out.println("Search not successful"); } } }
Output
Search successful Index of the word is: 30
The StringTokenizer class
Using the StringTokenizer class, you can divide a String into smaller tokens based on a delimiter and traverse through them. Following example tokenizes all the words in the source string and compares each word of it with the given word using the equals() method.
Example
import java.util.StringTokenizer; public class ParsingForSpecificWord { public static void main(String args[]) { String str1 = "Hello how are you welcome to Tutorialspoint"; String str2 = "Tutorialspoint"; //Instantiating the StringTookenizer class StringTokenizer tokenizer = new StringTokenizer(str1," "); int flag = 0; while (tokenizer.hasMoreElements()) { String token = tokenizer.nextToken(); if (token.equals(str2)){ flag = 1; } else { flag = 0; } } if(flag==1) System.out.println("Search successful"); else System.out.println("Search not successful"); } }
Output
Search successful
- Related Articles
- How to test if a string contains specific words in Java?
- How to scan a string for specific characters in Python?
- Parse a string to a Boolean object in Java
- How to search a MySQL table for a specific string?
- How to parse a JSON string using Streaming API in Java?
- How to search for a pattern in a Java string?
- How to Find a Specific String or Word in Files and Directories in Linux
- How to replace all occurrences of a word in a string with another word in java?
- How to check that a string is parse-able to a double in java?
- How to search for a string in an ArrayList in java?
- Remove specific word in a comma separated string with MySQL
- How to match a particular word in a string using Pattern class in Java?
- Query in MySQL for string fields with a specific length?
- How to check a String for palindrome using arrays in java?
- Java program to count words in a given string
