indexOf( ) and lastIndexOf( ) in Java
By: Hong
The String
class provides two
methods that allow you to search a string for a specified character or substring:
• indexOf( ) Searches
for the first occurrence of a character or substring.
• lastIndexOf( ) Searches
for the last occurrence of a character or substring.
These two methods are overloaded in several different ways. In all cases, the methods return the index at which the character or substring was found, or –1 on failure.
To search for the first occurrence of a character, use int indexOf(int ch)
To search for the last occurrence of a character, use int lastIndexOf(int ch)
Here, ch is the character being sought. To search for the first or last occurrence of a substring, use
int indexOf(String str)
int lastIndexOf(String str)
Here, str
specifies the substring.
You can specify a starting point for the search using these
forms:
int indexOf(int ch,
int startIndex)
int lastIndexOf(int ch,
int startIndex)
int indexOf(String str,
int startIndex)
int lastIndexOf(String str,
int startIndex)
Here, startIndex
specifies the index at
which point the search begins. For indexOf(
), the search runs from startIndex
to the end of the string.
For lastIndexOf( ),
the search runs from startIndex
to zero.
The following example shows how to use the various index methods
to search inside of
Strings:
// Demonstrate indexOf() and lastIndexOf().
class indexOfDemo {
public static void main(String args[]) {
String s = "Now is the time for all good men " +
"to come to the aid of their country.";
System.out.println(s);
System.out.println("indexOf(t) = " +
s.indexOf('t'));
System.out.println("lastIndexOf(t) = " +
s.lastIndexOf('t'));
System.out.println("indexOf(the) = " +
s.indexOf("the"));
System.out.println("lastIndexOf(the) = " +
s.lastIndexOf("the"));
System.out.println("indexOf(t, 10) = " +
s.indexOf('t', 10));
System.out.println("lastIndexOf(t, 60) = " +
s.lastIndexOf('t', 60));
System.out.println("indexOf(the, 10) = " +
s.indexOf("the", 10));
System.out.println("lastIndexOf(the, 60) = " +
s.lastIndexOf("the", 60));
}
}
Here is the output of this program:
Now is the time for all good men to come to the aid of their country.
indexOf(t) = 7
lastIndexOf(t) = 65
indexOf(the) = 7
lastIndexOf(the) = 55
indexOf(t, 10) = 11
lastIndexOf(t, 60) = 55
indexOf(the, 10) = 44
lastIndexOf(the, 60) = 55
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. Hi its good for the learners and thosse who forgot
View Tutorial By: Md.Abdul Gaffar at 2008-01-12 06:00:14
2. http://www.java-samples.com/showtutorial.php?tutor
View Tutorial By: Prab at 2009-04-13 11:54:17
3. so what about, if i need to remove these letters f
View Tutorial By: need Help at 2009-11-12 08:40:31
4. @Prab, no its 7 since it starts the counting at 0
View Tutorial By: Shadowfaux at 2009-11-21 15:22:49
5. To "need help":
String st
View Tutorial By: alex at 2010-05-13 09:57:25
6. To "need help":
String s
View Tutorial By: Mike at 2010-10-14 20:09:31
7. what's the purpose of indexOf('t',10)
View Tutorial By: pevi at 2011-02-22 04:23:20
8. Inamiccackack
View Tutorial By: Inamiccackack at 2011-09-09 12:20:09
9. Re: indexOf('t',10)
The search look for ins
View Tutorial By: Rick Hall at 2011-09-29 18:42:33
10. ASNWERING: what's the purpose of indexOf('t',10)
View Tutorial By: Kyle at 2012-03-22 09:17:04
11. Super. I found in some second what I was looking f
View Tutorial By: Urlaub in Ungarn at 2012-10-25 15:09:10
12. import java.io.*;
class lastindexof
View Tutorial By: arya at 2012-11-07 08:37:59
13. thanks for sharing the usage of indexof() method o
View Tutorial By: Rai at 2013-08-30 15:16:52
14. its cool, you've cleared me.
View Tutorial By: zealous at 2013-09-10 17:40:26
15. indexOf and contains methods are typically used fo
View Tutorial By: Sandeep at 2014-08-25 02:19:53
16. u can try this out it will satisify u:)
imp
View Tutorial By: vallabh at 2014-10-04 17:44:18
17. can anybody suggest me to write a java prog. to ke
View Tutorial By: Giri at 2015-03-23 17:56:58
18. can you please tell me how to find 2nd last index
View Tutorial By: Tabish at 2016-02-24 16:26:49
19. Idateyzek
View Tutorial By: Idateyzek at 2017-02-20 18:03:27
20. Jamestox
View Tutorial By: Jamestox at 2017-03-30 19:28:49
21. Hello blogger i see you don't monetize your websit
View Tutorial By: CharliX at 2017-08-09 22:49:29
22. nazmroCep
View Tutorial By: nkwaunCep at 2017-09-05 12:11:35
23. ntprlbCep
View Tutorial By: njfdppCep at 2017-09-13 03:20:00