Monday 7 May 2012

Java String lastIndexOf Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
String class lastIndexOf method example.

String class lastIndexOf method examle:- This example demonstrates the working of lastIndexOf method. 
This method returns the index or number location of letter from last of the statement.

Syntax:-lastIndexOf(String str) 


Here is the code:-
/**
 * @(#) LastIndexOfString.java 
 * LastIndexOfString class demonstrates the working of lastIndexOf() method of String class of lang package
 */

public class LastIndexOfString {
  public static void main(String args[]) {

    String prabhu = "o paalan haari", bholenaath;
    // double quotes as arguments inside parentheses
    // Method returns last index of letter or word passes in single ang
    System.out.println("last index of letter 'i':  "
        + prabhu.lastIndexOf('i'));

    bholenaath = "bam bolo bam bolo bam bam bam";
    // here instead of index of word 'bolo', the letter which word stats
    // that is 'b', is returned and same happens every time for every word
    // or statement
    System.out.println("Index of letter 'b' is returned:  "
        + bholenaath.lastIndexOf("bolo"));
    // here were complete statement is passed in the arguments but still
    // methos returns the index of first letter only
    System.out.println("Index of letter 'o' is returned:  "
        + prabhu.lastIndexOf("o paalan haari"));
  }
}
Output of the program:-
--------------------------------
last index of letter ' i ':  13
Index of letter 'b' is returned:  13
Index of letter 'o' is returned:  0

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...