Denne funksjonen brukes til å finne en spesifisert delstreng.
Syntaks
Tenk på to strenger str1 og str2. Syntaks vil være:
str1.find(str2);
Parametere
str : String som skal søkes etter.
pos : Den definerer posisjonen til tegnet for å starte søket.
n : Antall tegn i en streng det skal søkes etter.
ch : Den definerer karakteren som skal søkes etter.
Returverdi
Den returnerer posisjonen til det første tegnet i første kamp.
Eksempel 1
La oss se det enkle eksemplet.
#include using namespace std; int main() { string str= 'java is the best programming language'; cout << str<<'
'; cout <<' position of the programming word is :'; cout<< str.find('programming'); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Java is the best programming language Position of the programming word is 17 </pre> <h2>Example 2</h2> <p>Let's see simple example by passing position of a character as a parameter.</p> <pre> #include using namespace std; int main() { string str= 'Mango is my favorite fruit'; cout << str<<'
'; cout<< ' position of fruit is :'; str.find('fruit',12); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Mango is my favorite fruit Position of fruit is 21 </pre> <h2>Example 3</h2> <p>Let's see simple example of finding a single character.</p> <pre> #include using namespace std; int main() { string str = 'javatpoint'; cout << 'String contains :' << str; cout<< 'position of p is :' << str.find('p'); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> String contains : javatpoint Position of p is 5 </pre> <br></'
';></pre></'
';> Eksempel 2
La oss se et enkelt eksempel ved å sende posisjonen til et tegn som en parameter.
#include using namespace std; int main() { string str= 'Mango is my favorite fruit'; cout << str<<\'
\'; cout<< \' position of fruit is :\'; str.find(\'fruit\',12); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Mango is my favorite fruit Position of fruit is 21 </pre> <h2>Example 3</h2> <p>Let's see simple example of finding a single character.</p> <pre> #include using namespace std; int main() { string str = 'javatpoint'; cout << 'String contains :' << str; cout<< 'position of p is :' << str.find('p'); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> String contains : javatpoint Position of p is 5 </pre> <br></\'
\';> Eksempel 3
La oss se et enkelt eksempel på å finne et enkelt tegn.
#include using namespace std; int main() { string str = 'javatpoint'; cout << 'String contains :' << str; cout<< 'position of p is :' << str.find('p'); return 0; } Produksjon:
String contains : javatpoint Position of p is 5
\' \';>' ';>