OneBite.Dev - Coding blog in a bite size

find the position of the first occurrence of a substring in a string in java

Code snippet on how to find the position of the first occurrence of a substring in a string in java

  int indexOfSubstring = str.indexOf("substring");

This code finds the position of the first occurrence of a substring in a string. The indexOf() method takes a String as an argument and returns the position of the first occurrence. It returns -1 if the substring is not found in the string. In this example, the return value is assigned to the int variable indexOfSubstring.

java