Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java String indexOf Example

  1. /*
  2. Java String indexOf example.
  3. This Java String indexOf example describes how Java String indexOf method is used to
  4. find particular character or string within given string object.
  5. */
  6.  
  7.  
  8. public class JavaStringIndexOfExample{
  9.  
  10.  
  11. public static void main(String args[]){
  12.  
  13.  
  14. /*
  15. Java String class defines following couple of indexOf methods to
  16. search character or string within the given string.
  17. */
  18.  
  19.  
  20. String str = "Hello World World";
  21. String anotherString = "World";
  22.  
  23.  
  24. /*
  25. int indexOf(int ch) method returns the index of the first occurrence of the
  26. given character within given string.
  27. */
  28.  
  29.  
  30. System.out.println( str.indexOf('o') );
  31.  
  32.  
  33. /*
  34. int indexOf(int ch, int fromIndex) method returns the index of the first occurrence of the
  35. given character after given index within given string.
  36. */
  37.  
  38.  
  39. System.out.println( str.indexOf('o',5) );
  40.  
  41.  
  42. /*
  43. int indexOf(String str) method returns the index of the first occurrence of the given string
  44. within given string.
  45. */
  46.  
  47.  
  48. System.out.println( str.indexOf(anotherString) );
  49.  
  50.  
  51. /*
  52. int indexOf(String str, int fromIndex) method returns the index of the first occurrence of the
  53. given string after given index within given string.
  54. */
  55.  
  56.  
  57. System.out.println( str.indexOf(anotherString,11) );
  58.  
  59.  
  60. /*
  61. int lastIndexOf(int ch) method returns the index of the first occurrence of the
  62. given character. it searches the character in the backward direction staring from the end.
  63. */
  64.  
  65.  
  66. System.out.println( str.lastIndexOf('o') );
  67.  
  68.  
  69. /*
  70. int lastIndexOf(int ch, int fromIndex) method returns the index of the first occurrence of the
  71. given character. it searches the character in the backward direction staring from the given fromIndex.
  72. */
  73.  
  74.  
  75. System.out.println(  str.lastIndexOf('o',5) );
  76.  
  77.  
  78. /*
  79. int lastIndexOf(String str) method returns the index of the first occurrence of the
  80. given string. it searches the string in the backward direction staring from the given fromIndex.
  81. */
  82.  
  83.  
  84. System.out.println( str.lastIndexOf(anotherString) );
  85.  
  86.  
  87. /*
  88. int lastIndexOf(String str, int fromIndex) method returns the index of the first occurrence of the
  89. given string. it searches the string in the backward direction staring from the given fromIndex.
  90. */
  91.  
  92.  
  93. System.out.println( str.lastIndexOf(anotherString,11) );
  94.  
  95.  
  96. }
  97.  
  98.  
  99. }
  100.  
  101.  
  102. /*
  103.  
  104. OUTPUT of the above given Java String indexOf Example would be:
  105. 4
  106. 7
  107. 6
  108. 12
  109. 13
  110. 4
  111. 12
  112. 6
  113. */
Your rating: None Average: 4 (5 votes)
Share this

Post new comment

7 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.