top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find the position of the first occurrence of a substring in a string?

+5 votes
548 views
How to find the position of the first occurrence of a substring in a string?
posted Dec 31, 2015 by Vrije Mani Upadhyay

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

strpos(): To the position of the first occurrence of a substring in a string.

stripos() examples:

<?php
$findme    = 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';

$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);

// Nope, 'a' is certainly not in 'xyz'
if ($pos1 === false) {
    echo "The string '$findme' was not found in the string '$mystring1'";
}

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' is the 0th (first) character.
if ($pos2 !== false) {
    echo "We found '$findme' in '$mystring2' at position $pos2";
}
?>
answer Jan 2, 2016 by Amit Kumar Pandey
strpos() is used to find the position of the first occurrence of a substring in a string
Similar Questions
0 votes

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

0 votes

Find the first non repeating character in a string.

For example
Input string: "abcdeabc"
Output: "e"

+1 vote

How to find first unrepeated character in string using C/C++?

Input       Output     
abc         a    
aabcd       b
aabddbc     c
–1 vote

Write a C program to check if the given string is repeated substring or not.
ex
1: abcabcabc - yes (as abc is repeated.)
2: abcdabababababab - no

...