C++ tutorial-find() function


C++ String Manipulation

C++ find() function

You can use the find(string,[position]) function to find a substring in another string. The position argument specified the starting position of finding process in the invoking string. If you do not specify this argument the finding process will start at the beginning of the invoking string. The string argument is the string that you are looking for in the invoking string. This function returns the position of the matched string in the invoking string. If the target string is not found it returns -1.



Example:

#include <cstdlib>
#include <iostream>
#include <conio.h>


using namespace std;


void strmanip(){

  string str="C++ code programming";
  int i=str.find("code",3); //find string "code" in string str starting from position 3

  if(i!=-1)
    cout<<"\ncode was found at:"<<i<<endl;

  else cout<<"\ncode was not found"<<endl

}

int main(){

  strmanip();
  getch();
  return 0;

}




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.