- /*
- Java Search String using indexOf Example
- This example shows how we can search a word within a String object using
- indexOf method.
- */
- public class SearchStringExample {
- public static void main(String[] args) {
- //declare a String object
- String strOrig = "Hello world Hello World";
- /*
- To search a particular word in a given string use indexOf method.
- indexOf method. It returns a position index of a word within the string
- if found. Otherwise it returns -1.
- */
- int intIndex = strOrig.indexOf("Hello");
- if(intIndex == - 1){
- System.out.println("Hello not found");
- }else{
- System.out.println("Found Hello at index " + intIndex);
- }
- /*
- we can also search a word after particular position using
- indexOf(String word, int position) method.
- */
- int positionIndex = strOrig.indexOf("Hello",11);
- System.out.println("Index of Hello after 11 is " + positionIndex);
- /*
- Use lastIndexOf method to search a last occurrence of a word within string.
- */
- int lastIndex = strOrig.lastIndexOf("Hello");
- System.out.println("Last occurrence of Hello is at index " + lastIndex);
- }
- }
- /*
- Output of the program would be :
- Found Hello at index 0
- Index of Hello after 11 is 12
- Last occurrence of Hello is at index 12
- */
Welcome to the world of Java examples, organized by categories and Java packages. Java examples (Java sample source code) help to understand functionality of various Java classes and methods as well as various programming techniques in a simple way, which is otherwise very hard to learn by reading tutorials or Java API. So start exploring...
Tuesday, November 29, 2011
Java Search String using indexOf Example
Subscribe to:
Post Comments (Atom)
How to find the most appropriate Keywords?
🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...
-
/* Pause Thread Using Sleep Method Example This Java example shows how to pause currently running thread using sleep method of Jav...
-
/* Java String split example. This Java String split example describes how Java String is split into multiple Java String objects. */ ...
-
// File Name GreetingClient.java import java.net.*; import java.io.*; public class GreetingClient { public static void main(String [] ...
No comments:
Post a Comment