Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- Java String indexOf example.
- This Java String indexOf example describes how Java String indexOf method is used to
- find particular character or string within given string object.
- */
- public class JavaStringIndexOfExample{
- public static void main(String args[]){
- /*
- Java String class defines following couple of indexOf methods to
- search character or string within the given string.
- */
- String str = "Hello World World";
- String anotherString = "World";
- /*
- int indexOf(int ch) method returns the index of the first occurrence of the
- given character within given string.
- */
- System.out.println( str.indexOf('o') );
- /*
- int indexOf(int ch, int fromIndex) method returns the index of the first occurrence of the
- given character after given index within given string.
- */
- System.out.println( str.indexOf('o',5) );
- /*
- int indexOf(String str) method returns the index of the first occurrence of the given string
- within given string.
- */
- System.out.println( str.indexOf(anotherString) );
- /*
- int indexOf(String str, int fromIndex) method returns the index of the first occurrence of the
- given string after given index within given string.
- */
- System.out.println( str.indexOf(anotherString,11) );
- /*
- int lastIndexOf(int ch) method returns the index of the first occurrence of the
- given character. it searches the character in the backward direction staring from the end.
- */
- System.out.println( str.lastIndexOf('o') );
- /*
- int lastIndexOf(int ch, int fromIndex) method returns the index of the first occurrence of the
- given character. it searches the character in the backward direction staring from the given fromIndex.
- */
- System.out.println( str.lastIndexOf('o',5) );
- /*
- int lastIndexOf(String str) method returns the index of the first occurrence of the
- given string. it searches the string in the backward direction staring from the given fromIndex.
- */
- System.out.println( str.lastIndexOf(anotherString) );
- /*
- int lastIndexOf(String str, int fromIndex) method returns the index of the first occurrence of the
- given string. it searches the string in the backward direction staring from the given fromIndex.
- */
- System.out.println( str.lastIndexOf(anotherString,11) );
- }
- }
- /*
- OUTPUT of the above given Java String indexOf Example would be:
- 4
- 7
- 6
- 12
- 13
- 4
- 12
- 6
- */

Compelling entanglement term
Post new comment