-
/*Java Search String using indexOf ExampleThis example shows how we can search a word within a String object usingindexOf method.*/
public class SearchStringExample
{
public static void main(String[] args)
{//declare a String objectString 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 stringif 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 usingindexOf(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 0Index of Hello after 11 is 12Last occurrence of Hello is at index 12*/
Friday, 13 April 2018
Popular Posts
-
Build interactive, data-driven websites with the potent combination of open-source technologies and web standards, even if you have only ...
-
Java :- It is a fast, secure and reliable general purpose computer programming language. Python :- A Readable, efficient and powerfu...
-
Business Analytics Definition "Study of business data using statistical technique and programming for creating decision suppo...
-
Variables in R A variables are nothing but reserved memory locations to store values. This means that when you create a variab...
-
What is Python? → Python is an interpreted High level programming Language for General purpose programming. Python is created by Gui...
-
Why do we need Analytics ? → Data analytics helps organizations harness their data and use it to identify few opportunities. - Cost re...
-
- > is the prompt sign in R - The assignment operators are the left arrow with dash <- and equal sign =. > x <- 20 ...
-
Java is develop by James Gosling in 1991 Java is a developed in the early 90s in Sun Microsystems company. Java first version release in ...
-
Importing Data Files Spreadsheet (Excel) file data The xlsx package has the function read.xlsx ( ) for reading Excel files. This ...
-
1. Simple & Easy To Learn Open Source High-level Interpreted Large community 2. Portable & Extensible 3. Web ...

0 Comments:
Post a Comment