What is used to find the first occurrence of a given string in another string?

❮ PHP String Reference

Example

Find the position of the first occurrence of "php" inside the string:

echo strpos("I love php, I love php too!","php");
?>

Try it Yourself »


Definition and Usage

The strpos() function finds the position of the first occurrence of a string inside another string.

Note: The strpos() function is case-sensitive.

Note: This function is binary-safe.

Related functions:

  • strrpos() - Finds the position of the last occurrence of a string inside another string (case-sensitive)
  • stripos() - Finds the position of the first occurrence of a string inside another string (case-insensitive)
  • strripos() - Finds the position of the last occurrence of a string inside another string (case-insensitive)

Syntax

strpos(string,find,start)

Parameter Values

ParameterDescriptionstringRequired. Specifies the string to searchfindRequired. Specifies the string to findstartOptional. Specifies where to begin the search. If start is a negative number, it counts from the end of the string.

Technical Details

Return Value:Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found. Note: String positions start at 0, and not 1.PHP Version:4+Changelog:PHP 7.1.0 - The start parameter can be a negative number
❮ PHP String Reference

where string is the string in which you have to find the index of first occurrence of substring. start and end are optional and are starting and ending positions respectively in which substring has to be found.

Example 1: Find index of First Occurrence of Substring

In the following example, we will take a string and try to find the first occurrence of substring

string = 'Python programming. Network programming.'
substring = 'prog'

index = string.find(substring)
print(index)
0.

Python Program

string = 'Python programming. Network programming.'
substring = 'prog'

index = string.find(substring)
print(index)
Run

Output

7

Example 2: Find index of First Occurrence of Substring after a specific position

In the following example, we will take a string and try to find the first occurrence of substring

string = 'Python programming. Network programming.'
substring = 'prog'

index = string.find(substring)
print(index)
0 after position
string = 'Python programming. Network programming.'
substring = 'prog'

index = string.find(substring)
print(index)
2 in the string.

Python Program

string = 'Python programming. Network programming.'
substring = 'prog'
start = 12

index = string.find(substring, start)
print(index)
Run

Output

28

Now it has found index of the substring that occurred for the first time after start position.

Summary

In this tutorial of Python Examples, we learned how to find the index of substring in a string using str.find() function.

In this section you can learn and practice C Programming Questions based on "Strings" and improve your skills in order to face the interview, competitive examination and various entrance test (CAT, GATE, GRE, MAT, Bank Exam, Railway Exam etc.) with full confidence.

Where can I get C Programming Strings questions and answers with explanation?

IndiaBIX provides you lots of fully solved C Programming (Strings) questions and answers with Explanation. Solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download C Programming Strings quiz questions with answers as PDF files and eBooks.

Where can I get C Programming Strings Interview Questions and Answers (objective type, multiple choice)?

Here you can find objective type C Programming Strings questions and answers for interview and entrance examination. Multiple choice and true or false type questions are also provided.

How to solve C Programming Strings problems?

You can easily solve all kind of C Programming questions based on Strings by practicing the objective type exercises given below, also get shortcut methods to solve C Programming Strings problems.

 std::find from C++ STL, the index method in Python, the indexOf method in Java, the indexOf method in JavaScript are some inbuilt functions in the libraries of respective languages for finding the starting index of a substring in a given string. 

To find the index of first occurrence of a substring in a string you can use String.indexOf() function.

A string, say str2, can occur in another string, say str1, n number of times. There could be a requirement in your Java application, that you have to find the position of the first occurrence of str2 in str1. Or you may need to find nth occurrence.

In this tutorial, we shall learn to get the index of first occurrence of a string in another string using Java.

We shall make use of String.indexOf(String otherString) method, to find the index of first occurrence of string str2 in str1. If the string str2 is present in the other string str1, it returns the index of its first occurrence. If it is not present, then it returns

str2 first appeared in str1 at index : 12
3 indicating that the string str2 is not present in the string str1.

Example – Get the index of first occurrence of a substring

In this example, we shall initialize two strings with variable names str1 and str2. And we are going to find the first occurrence of str2 in str1.

/**
 * Java Example program to find the index of first occurrence of a substring in a string
 */
public class FirstOccurrenceExample {

	public static void main(String[] args) {
		//initialize strings
		String str1 = "hello world good morning. good day.";
		String str2 = "good";

		//get the index of str2 in str1
		int indexOfSubStr = str1.indexOf(str2);
		
		System.out.println("str2 first appeared in str1 at index : "+ indexOfSubStr);
	}
}

When the above program is run, the output to the console is as shown below :

str2 first appeared in str1 at index : 12

Example – Get the index of first occurrence of a substring – Ignore Case

In this example, we ignore the case of both the strings and try to find the occurrence of string str2 in string str1. To ignore the case, we have actually converted the strings to lowercase and then applied the function indexOf().

Please note that with the strings str1 and str2 in the below program, if you consider the case, indexOf() would return 

str2 first appeared in str1 at index : 12
3. But as we are ignoring the case, we got
/**
 * Java Example program to find the index of first occurrence of a substring in a string
 */
public class FirstOccurrenceExample {

	public static void main(String[] args) {
		//initialize strings
		String str1 = "hello world GoOd morning. gOOD day.";
		String str2 = "GOOD";

		//ignore case and get the index of str2 in str1
		int indexOfSubStr = str1.toLowerCase().indexOf(str2.toLowerCase());
		
		System.out.println("str2 first appeared in str1 at index : "+ indexOfSubStr);
	}
}
3 and
/**
 * Java Example program to find the index of first occurrence of a substring in a string
 */
public class FirstOccurrenceExample {

	public static void main(String[] args) {
		//initialize strings
		String str1 = "hello world GoOd morning. gOOD day.";
		String str2 = "GOOD";

		//ignore case and get the index of str2 in str1
		int indexOfSubStr = str1.toLowerCase().indexOf(str2.toLowerCase());
		
		System.out.println("str2 first appeared in str1 at index : "+ indexOfSubStr);
	}
}
4 in str1 matched.

/**
 * Java Example program to find the index of first occurrence of a substring in a string
 */
public class FirstOccurrenceExample {

	public static void main(String[] args) {
		//initialize strings
		String str1 = "hello world GoOd morning. gOOD day.";
		String str2 = "GOOD";

		//ignore case and get the index of str2 in str1
		int indexOfSubStr = str1.toLowerCase().indexOf(str2.toLowerCase());
		
		System.out.println("str2 first appeared in str1 at index : "+ indexOfSubStr);
	}
}

Run the program.

str2 first appeared in str1 at index : 12

Conclusion

In this Java Tutorial, we have learned how to get index of first occurrence of a substring in a String, by considering or ignoring the case of alphabets.

Which function is used to find the first occurrence of the given string in another string?

The strchr() function finds the first occurrence of a character in a string. The character c can be the null character (\0); the ending null character of string is included in the search. The strchr() function operates on null-ended strings.

Which method is used to find the occurrence a string within another string?

If the substring is present in the string, find() returns the index, or the character position, of the first occurrence of the specified substring from that given string. If the substring you are searching for is not present in the string, then find() will return -1 .

How to find first occurrence of a string in a string Python?

Python String find() method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1.