TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java lastIndexOf Examples

Call lastIndexOf with chars and String arguments. Search from the end of a String.
LastIndexOf. A string contains the letters "abc." But this occurs twice in the string. With lastIndexOf we search from the rightmost index of a String.indexOf
With an index argument, we begin searching from a certain position in the string. In a while-loop, we can scan for all occurrences of a string from the end of the source string.
Method example. This simple example uses the lastIndexOf method with char arguments. It locates first the final instance of the char.

Info: As with indexOf, lastIndexOf can accept a String and a starting index as arguments. It mirrors indexOf in these ways.

Strings

Here: We find the last "s" in the string "spins." But the character "x" is not present in the string.

Java program that uses lastIndexOf public class Program { public static void main(String[] args) { String line = "spins"; // Get index of last "s." int result1 = line.lastIndexOf('s'); System.out.println(result1); // There is no "x" in the string. // ... This returns negative 1. int result2 = line.lastIndexOf('x'); System.out.println(result2); } } Output 4 -1
While, search strings. Here we call lastIndexOf with a string argument in a while-loop. The lastIndexOf call locates the last "cat" string at a starting position.

Argument 1: This is the string (or character) we are searching for in the source string.

Argument 2: This is the starting position. With lastIndexOf we begin searching from this index to the left.

Tip: At the end of the while-loop we must decrement the index variable i to continue searching the string to the left.

Java program that uses lastIndexOf in while-loop public class Program { public static void main(String[] args) { // The string we are searching. String line = "cat cat;cat"; // Search from this index (the last index). int i = line.length() - 1; // Continue calling lastIndexOf from the last start position. while ((i = line.lastIndexOf("cat", i)) >= 0) { // The string "cat" was found. // ... It starts at this position. System.out.println(line.substring(i)); // Move to previous index. i--; } } } Output cat cat;cat cat cat;cat
Benchmark, lastIndexOf. Sometimes we can use lastIndexOf as an optimization. If a substring occurs 0 or 1 times in a String, indexOf and lastIndexOf return the same result.

Version 1: This code calls the indexOf method, so it searches for the char 8 from the start of the string.

Version 2: This version of the code uses lastIndexOf, so it searches for the char from the string's end instead.

Result: If, based on our data, we find that the value occurs near the end more often, lastIndexOf is a faster yet equal method.

Java program that times indexOf, lastIndexOf public class Program { public static void main(String[] args) { String input = "0123456789"; long t1 = System.currentTimeMillis(); // Version 1: use indexOf. for (int i = 0; i < 10000000; i++) { int index = input.indexOf('8'); if (index < 0) { System.out.println(false); } } long t2 = System.currentTimeMillis(); // Version 2: use lastIndexOf. for (int i = 0; i < 10000000; i++) { int index = input.lastIndexOf('8'); if (index < 0) { System.out.println(false); } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(t2 - t1); System.out.println(t3 - t2); } } Output 41 ms, indexOf 14 ms, lastIndexOf
A review. The lastIndexOf method searches from the right to the left. It is essentially the same method as indexOf but with an inverted order of searching.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf