How do you find the position of the first occurrence of a substring in a string in C?

Example

Search for "3" in string "W3Schools.com", and return position:

SELECT POSITION("3" IN "W3Schools.com") AS MatchPosition;

Try it Yourself »


Definition and Usage

The POSITION() function returns the position of the first occurrence of a substring in a string.

If the substring is not found within the original string, this function returns 0.

This function performs a case-insensitive search.

Note: The LOCATE() function is equal to the POSITION() function.

Syntax

POSITION(substring IN string)

Parameter Values

ParameterDescription
substring Required. The substring to search for in string
string Required. The original string that will be searched

Technical Details

Works in:From MySQL 4.0

More Examples

Example

Search for "COM" in string "W3Schools.com", and return position:

SELECT POSITION("COM" IN "W3Schools.com") AS MatchPosition;

Try it Yourself »

Example

Search for "a" in CustomerName column, and return position:

SELECT POSITION("a" IN CustomerName)
FROM Customers;

Try it Yourself »



Examples

Search a string for "welcome":

let text = "Hello world, welcome to the universe.";
let result = text.indexOf("welcome");

Try it Yourself »

Search a string for "Welcome":

let text = "Hello world, welcome to the universe.";
let result = text.indexOf("Welcome");

Try it Yourself »

Find the first occurrence of "e":

let text = "Hello world, welcome to the universe.";
text.indexOf("e");

Try it Yourself »

Find the first occurrence of "e", starting at position 5:

let text = "Hello world, welcome to the universe.";
text.indexOf("e", 5);

Try it Yourself »

Find the first occurrence of "a":

let text = "Hello world, welcome to the universe.";
text.indexOf("a");

Try it Yourself »


Definition and Usage

The indexOf() method returns the position of the first occurrence of a value in a string.

The indexOf() method returns -1 if the value is not found.

The indexOf() method is case sensitive.


Syntax

string.indexOf(searchvalue, start)

Parameters

Parameter Description
searchvalue Required.
The string to search for.
start Optional.
The position to start from (default is 0).

Return Value

Type Description
A number The first position where the search-value occurs.
-1 if it never occurs.


The indexOf() method cannot search against a regular expression.

The search() cannot take a start position argument.



Browser Support

indexOf() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes



MySQL LOCATE() function makes it possible to find the location of the first occurrence of a substring in a string. Following is the syntax of using it −

Syntax

LOCATE(Substring, String)

In this function, Substring is the string whose position of occurrence needs to find and the string is a string from which the occurrence of substring needs to be searched.

We must have to pass both the strings (i.e. substring, which is to be searched and the string, from which substring is to be searched) as arguments of the LOCATE() function.

Example

mysql> Select LOCATE('Good','RAM IS A GOOD BOY')As Result;
+--------+
| Result |
+--------+
|     10 |
+--------+
1 row in set (0.00 sec)

In the above example, the output is 10 because the first occurrence of substring ’good’ start from 10th position in the string ‘RAM IS A GOOD BOY’.

However, we can also manage the starting point of searching with the help of another argument representing position. The syntax of LOCATE() with position argument would be as follows −

LOCATE(Substring, String, position)

Example

mysql> Select LOCATE('Good','Good, RAM IS A GOOD BOY')As Result;
+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

mysql> Select LOCATE('Good','Good, RAM IS A GOOD BOY',5)As Result;
+--------+
| Result |
+--------+
|     16 |
+--------+
1 row in set (0.00 sec)

From the above result set, we can see the difference between using position argument. If we do not use it the search by default starts from the 1st position and when we specify ‘5’ as the position argument in our example it starts from 5th position.

How do you find the position of the first occurrence of a substring in a string in C?

Updated on 04-Feb-2020 05:50:33

  • Related Questions & Answers
  • Get the first occurrence of a substring within a string in Arduino
  • How to find the nth occurrence of substring in a string in Python?
  • Get the substring after the first occurrence of a separator in Java
  • How to find index of last occurrence of a substring in a string in Python?
  • How it is possible in MySQL to find a string of specified pattern within another string?
  • Get the last occurrence of a substring within a string in Arduino
  • Insert the results of a MySQL select? Is it possible?
  • How to get the first index of an occurrence of the specified value in a string in JavaScript?
  • Get the index of the first occurrence of a separator in Java
  • Get the substring before the last occurrence of a separator in Java
  • How to find the first character of a string in C#?
  • How to find the location of a string in an R data frame?
  • How to count the occurrence of a specific string in a string in JavaScript
  • PHP – iconv_strpos() function – Find the position of the first occurrence of a needle in a haystack
  • How to find the first 10 characters of a string in C#?

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

The indexOf() method returns the position of the first occurrence of specified character(s) in a string.

How do you find the position of the first occurrence of a substring in a string in 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.

How do you find the location of a substring in a string in C?

Find Index of Substring in String in C Language To find the index of given substring in a string, iterate over the indices of this string and check if there is match with the substring from this index of this string in each iteration.

How do you find first occurrence of a string in a string c?

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.