>
Click HERE for the Comparison Methods of String class.
Click HERE for the Substring Methods of String class.
The String Class: (Part 4)
Indexing Methods of String class:

- Example: (indexOf -> ch -> two arguments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Good Morning!"; System.out.println(str1.indexOf('o',5)); // prints 6. Index of ‘o’ after after index 5. } } - Example: (indexOf -> ch -> one argument)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Good Morning!"; System.out.println(str1.indexOf('o')); // prints 1. The first Index of ‘o’. } } - Example: (indexOf -> string -> two arguments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Good Moorning!"; System.out.println(str1.indexOf("oo",2)); // prints 6. Index of string “oo” after after index 5. } } - Example: (indexOf -> string -> one argument)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Good Moorning!"; System.out.println(str1.indexOf("oo")); // prints 1. The first Index of string “oo”. } } - lastIndexOf methods resemble indexOf, only difference is -> they search for the last occurrences instead of the first occurrences.
Example: All lastIndexOf Methods:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters"Welcome to Java".lastIndexOf('W') returns 0. [one ch argument] "Welcome to Java".lastIndexOf('o') returns 9. [one ch argument] "Welcome to Java".lastIndexOf('o', 5) returns 4. [two arguments] "Welcome to Java".lastIndexOf("come") returns 3. [one string argument] "Welcome to Java".lastIndexOf("Java", 5) returns -1. [two arguments ] "Welcome to Java".lastIndexOf("Java") returns 11. [one string]
Click HERE for the Comparison Methods of String class.
Click HERE for the Substring Methods of String class.
Comments
Post a Comment