Don't substring twice. Use the found index twice instead:

int idx = example.lastIndexOf("id\":\"");
example = example.substring(idx + 5, idx + 13);

Or, if the length is dynamic, but always ends with -:

int start = example.lastIndexOf("id\":\"");
int end = example.indexOf('-', start);
example = example.substring(start + 5, end);

In real code, you should of course always check that the substring is found at all, i.e. that idx / start / end are not -1.

Answer from Andreas on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-string-lastindexof-method
Java String lastIndexOf() Method with Examples - GeeksforGeeks
November 19, 2024 - Last index of char or substring. Now, lets see examples of all 4 variants of lastIndexOf() method of String class. To demonstrate how to find the last index of a character in a string. Java ·
🌐
Programiz
programiz.com › java-programming › library › string › lastIndexOf
Java String lastIndexOf()
The last occurrence of 'r' in the "Learn Java programming" string is at index 15. However, str1.lastIndexOf('r', 4) searches the substring "Learn".
🌐
W3Schools
w3schools.com › java › ref_string_lastindexof.asp
Java String lastIndexOf() Method
HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference AngularJS Reference jQuery Reference · HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › lastIndexOf
Java String lastIndexOf() - Find Last Index | Vultr Docs
December 18, 2024 - String unicodeStr = "funny bunny"; int lastIndexUnicode = unicodeStr.lastIndexOf(121); // Unicode for 'y' System.out.println("Last index of 'y' using Unicode: " + lastIndexUnicode); Explain Code · Here, the Unicode value 121 corresponds to ...
🌐
How to do in Java
howtodoinjava.com › home › string › java string lastindexof()
Java String lastIndexOf() with Examples - HowToDoInJava
January 11, 2023 - int lastIndexOf(String substring) int lastIndexOf(String substring, int fromIndex) int lastIndexOf(int ch) int lastIndexOf(int ch, int fromIndex) ... The method returns the index of the last occurrence of the specified substring, or -1 if there ...
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-lastindexof-method-example
Java String lastIndexOf() Method with example
In the following example we are searching few given characters and a given substring in the String str. We are looking for their last occurrence in the String str thats why we are using lastIndexOf() method. If you want to find out the first occurrence of a char or substring in a string then using indexOf() method instead. public class JavaExample { public static void main(String[] args) { String str = "beginnersbook is for beginners"; char ch = 'b'; char ch2 = 's'; String subStr = "beginners"; int posOfB = str.lastIndexOf(ch); int posOfS = str.lastIndexOf(ch2); int posOfSubstr = str.lastIndexOf(subStr); System.out.println(posOfB); System.out.println(posOfS); System.out.println(posOfSubstr); } } Output: Here we are demonstrating the use of lastIndexOf() method in various cases.
🌐
W3Resource
w3resource.com › java-tutorial › string › string_lastindexof.php
Java String: lastIndexOf Method - w3resource
This is an Example of lastIndexof : lastIndexOf(t, 60) = 25 · public int lastIndexOf(String str) Returns the index within this string of the last occurrence of the specified substring.
Find elsewhere
🌐
Quora
quora.com › How-do-you-use-substring-and-lastIndexOf-in-Java
How to use substring and lastIndexOf in Java - Quora
Answer: The substring() method extracts characters, between two indices (positions), from a string, and returns the substring. The substring() method extracts characters from start to end (exclusive). The substring() method does not change the original string. The lastIndexOf() method returns th...
🌐
DataFlair
data-flair.training › blogs › java-string-lastindexof-method
Java String lastIndexOf() Method with Examples - DataFlair
July 25, 2024 - Let’s see an example: public class LastIndexOfExample { public static void main(String[] args) { String str = "Hello World"; int result = str.lastIndexOf('o'); System.out.println("The last index of 'o' in the string is: " + result); } }
🌐
CodeGym
codegym.cc › java blog › strings in java › java string lastindexof() method
Java String lastIndexOf() Method
October 11, 2023 - public int lastIndexOf(String str, int beg) Parameters str: a string. fromIndex: the index to start the search from. Let's try to find the index of the last occurrence of the substring “ro” in the string “This is major Tom to ground control, do you copy”. ...
🌐
Educative
educative.io › answers › what-is-the-lastindexof-method-in-java
What is the lastIndexOf() method in Java?
The lastIndexOf() method in Java returns the index of the last occurrence of a given character or substring​. If the character or the substring is not found, ​ the ​function returns
🌐
TechVidvan
techvidvan.com › tutorials › java-string-lastindexof-method
Java String lastIndexOf() Method with Examples - TechVidvan
June 3, 2024 - If the substring does not occur in the string, the method returns-1. If the string is very large, The method can be slow. This has taught us the syntax for the Java lastIndexOf() function and, with the help of examples, how to utilise it.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_lastindexof_string.htm
Java.lang.String.lastIndexOf() Method
package com.tutorialspoint; import java.lang.*; public class StringDemo { public static void main(String[] args) { String str1 = "Collections of tutorials at tutorials point"; // returns index of first character of the last substring "tutorials" System.out.println("index = " + str1.lastInde...
🌐
Baeldung
baeldung.com › home › java › java string › java string.lastindexof()
Java.String.lastIndexOf() | Baeldung
April 11, 2025 - • Java String.lastIndexOf() (current article)• Java String.regionMatches() • Java String.replace() • Java String.replaceAll() • Java String.split() • Java String.startsWith() • Java String.subSequence() • Java String.substring() • Java String.toLowerCase() • Java String.toUpperCase() • Java String.trim() • Java String.valueOf() The method lastIndexOf() returns the index of the last occurrence of a String in another String.
🌐
Javatpoint
javatpoint.com › java-string-lastindexof
Java String lastIndexOf() method - javatpoint
Java String lastIndexOf() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string lastindexof in java etc.
🌐
Codecademy
codecademy.com › docs › java › strings › .lastindexof()
Java | Strings | .lastIndexOf() | Codecademy
March 2, 2023 - The .lastIndexOf() method searches a string for a specified value and returns the position of the match. It searches the string, from the end to the beginning, and returns the index of the specified value. In other words, it returns the index of the last occurrence of the specified value.
🌐
Tabnine
tabnine.com › home page › code › java › java.lang.string
Java Examples & Tutorials of String.lastIndexOf (java.lang) | Tabnine
1 : 0); int lastIndex = uri.lastIndexOf('.'); int end = (lastIndex < 0 ? uri.length() : lastIndex); return uri.substring(start, end); } ... /** * Determine the name of the class file, relative to the containing * package: e.g. "String.class" ...
🌐
Tutorialspoint
tutorialspoint.com › home › java › java string lastindexof method
Java String lastIndexOf Method
September 1, 2008 - int lastIndexOf(int ch) Here is the detail of parameters − · ch − a character. This method returns the index. import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome to Tutorialsp...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.lastindexof
String.LastIndexOf Method (Java.Lang) | Microsoft Learn
The returned index is the largest value k for which: {@code k <= Math.min(fromIndex, this.length()) && this.startsWith(str, k) } If no such value of k exists, then -1 is returned.