logo

C++ String Find()

Denne funktion bruges til at finde en specificeret understreng.

Syntaks

Overvej to strenge str1 og str2. Syntaks ville være:

 str1.find(str2); 

Parametre

str : Streng, der skal søges efter.

pos: Den definerer placeringen af ​​karakteren, hvor søgningen skal startes.

n: Antal tegn i en streng, der skal søges efter.

ch : Det definerer den karakter, der skal søges efter.

Returværdi

Det returnerer positionen for det første tegn i første kamp.

Eksempel 1

Lad os se det enkle eksempel.

 #include using namespace std; int main() { string str= &apos;java is the best programming language&apos;; cout &lt;&lt; str&lt;<'
'; 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&apos;s see simple example by passing position of a character as a parameter.</p> <pre> #include using namespace std; int main() { string str= &apos;Mango is my favorite fruit&apos;; cout &lt;&lt; str&lt;<'
'; 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&apos;s see simple example of finding a single character.</p> <pre> #include using namespace std; int main() { string str = &apos;javatpoint&apos;; cout &lt;&lt; &apos;String contains :&apos; &lt;&lt; str; cout&lt;&lt; &apos;position of p is :&apos; &lt;&lt; str.find(&apos;p&apos;); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> String contains : javatpoint Position of p is 5 </pre> <br></'
';></pre></'
';>

Eksempel 2

Lad os se et simpelt eksempel ved at overføre et tegns position som en parameter.

 #include using namespace std; int main() { string str= &apos;Mango is my favorite fruit&apos;; cout &lt;&lt; str&lt;<\'
\'; 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&apos;s see simple example of finding a single character.</p> <pre> #include using namespace std; int main() { string str = &apos;javatpoint&apos;; cout &lt;&lt; &apos;String contains :&apos; &lt;&lt; str; cout&lt;&lt; &apos;position of p is :&apos; &lt;&lt; str.find(&apos;p&apos;); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> String contains : javatpoint Position of p is 5 </pre> <br></\'
\';>

Eksempel 3

Lad os se et enkelt eksempel på at finde et enkelt tegn.

 #include using namespace std; int main() { string str = &apos;javatpoint&apos;; cout &lt;&lt; &apos;String contains :&apos; &lt;&lt; str; cout&lt;&lt; &apos;position of p is :&apos; &lt;&lt; str.find(&apos;p&apos;); return 0; } 

Produktion:

 String contains : javatpoint Position of p is 5