-
/*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
-
An Essential Guide for Aspiring Machine Learning Developers If you're diving into the world of machine learning using Python , few re...
-
A Comprehensive Free Book by Bernd Klein If you're looking to dive deep into data analysis using Python , then "Data Analysis with...
-
If you're a beginner looking to dive into data science without getting lost in technical jargon or heavy theory, Elements of Data Scie...
-
MITx: Introduction to Computer Science and Programming Using Python — A Gateway into the World of Computing In the age of digital transforma...
-
Exploring the Course: Web Development for Beginners Specialization In the modern digital era, having a solid understanding of web developm...
-
Step-by-Step Explanation ➤ Step 1: Variable Values a = True b = False c = False ➤ Step 2: Understand the condition if a or b and c: Pyt...
-
Step-by-Step Explanation ✅ Line 1: import array This imports Python's built-in array module, which provides an efficient way to sto...
-
Exploring the Coursera Course: An Introduction to Programming the Internet of Things (IoT) Specialization As technology continues to evolv...
-
🔍 Overview If you’ve ever searched for a rigorous and mathematically grounded introduction to data science and machine learning , then t...
-
Tx: Becoming an Entrepreneur — A Comprehensive Guide to Launching Your Startup Journey In today’s innovation-driven world, entrepreneurshi...
Categories
100 Python Programs for Beginner
(118)
AI
(60)
Android
(24)
AngularJS
(1)
Api
(2)
Assembly Language
(2)
aws
(20)
Azure
(8)
BI
(10)
book
(4)
Books
(224)
C
(78)
C#
(12)
C++
(83)
Course
(71)
Coursera
(282)
Cybersecurity
(26)
Data Analysis
(15)
Data Analytics
(10)
data management
(13)
Data Science
(172)
Data Strucures
(10)
Deep Learning
(24)
Django
(16)
Downloads
(3)
edx
(20)
Engineering
(15)
Euron
(29)
Events
(7)
Excel
(13)
Factorial
(1)
Finance
(8)
flask
(3)
flutter
(1)
FPL
(17)
Generative AI
(24)
Google
(42)
Hadoop
(3)
HTML Quiz
(1)
HTML&CSS
(48)
IBM
(39)
IoT
(3)
IS
(25)
Java
(96)
Java quiz
(1)
Leet Code
(4)
Machine Learning
(105)
Meta
(24)
MICHIGAN
(5)
microsoft
(8)
Nvidia
(8)
p
(1)
Pandas
(4)
PHP
(20)
Projects
(29)
pyth
(1)
Python
(1116)
Python Coding Challenge
(593)
Python Quiz
(203)
Python Tips
(5)
Questions
(2)
R
(71)
React
(6)
Scripting
(3)
security
(3)
Selenium Webdriver
(4)
Software
(18)
SQL
(44)
UX Research
(1)
web application
(11)
Web development
(5)
web scraping
(2)
0 Comments:
Post a Comment