Search Java Programs

Showing posts with label lastIndexOf(). Show all posts
Showing posts with label lastIndexOf(). Show all posts

Wednesday, January 20, 2010

Java String lastIndexOf Example

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
* @version 16-May-2008
* @author Rose India Team
*/


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

Website Design by Mayuri Multimedia