In this article we are going to discuss a problem from Java String; this is one of the most commonly asked interview question of today. Write a program in Java to identify the last occurrence of a substring inside a String. .
 

Write a program in Java to identify the last occurrence of a substring inside a String.

e.g. if given string is "tbNext is not only good, but very good " than find last occurance of good in the string.
Write a program in Java to identify the last occurrence of a substring inside a String, as shown in below code:
package com.tbNext.examples;

public class LastOccuranceOfASubstring {

	public static void main(String[] args) {
		String givenString = "tbNext is not only good, but very good";
		String subString = "good";

		int lastIndex = givenString.lastIndexOf(subString);
		System.out.println("Last index of " + subString + " : " + lastIndex);

	}

}


Output:

Last index of good : 37


That's it, we saw how to write a program in Java to identify the last occurrence of a substring inside a String. In upcoming articles we will see more about Java and Related programming problems and solutions.
  • By Techburps.com
  • Apr 16, 2016
  • Programming