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.
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".
Videos
Top answer 1 of 2
1
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.
2 of 2
0
You can use a regex to find the specific substring:
String regex = "^id[^a-z0-9]+([a-zA-Z0-9]+)-.*$";
Matcher p = Pattern.compile(regex).matcher(example);
String result = null;
if (p.matches()) {
result = p.group(1);
}
System.out.println(result); //outputs exactly "abcd1234"
The pattern uses a capturing group that matches id followed by non-alphanumeric characters and preceding -.
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
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
The following example shows the ... System.out.println(); System.out.println(str); System.out.println("lastIndexOf(t, 60) = " + str.lastIndexOf('t', 60)); System.out.println(); } } ... Returns the index within this string of ...
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.
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...
Scaler
scaler.com › topics › lastindexof-in-java
lastIndexOf() in Java - Scaler Topics
August 29, 2022 - Let's see an example of the lastIndexOf(). ... In the above example, first, we have declared the array of strings named arr. Now we are supposed to find the index of the last occurrence of the letter @ in each string of the array. After finding the index of the @ from the strings representing the email ids, we now print the name using substring() method.