C++ Tutorial/string/string find — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:30, 25 мая 2010

find first occurrence of a character - equivalent of strchr()

#include <iostream>
#include <string>
using namespace std;
int main( )
{
   string typing( "The quick, brown fox jumps over the lazy dog" );
   cout << "String:  " << typing << endl;
   // find first occurrence of a character - equivalent of strchr()
   string::size_type index = typing.find( "u" );
   if( index != string::npos )
      cout << "\nThe first \"u\" is at index " << index;
   else
      cout << "\nThere is no \"u\" in the string";
}

find first occurrence of a substring - equivalent of strstr()

#include <iostream>
#include <string>
using namespace std;
int main( )
{
   string typing( "The quick, brown fox jumps over the lazy dog" );
   cout << "String:  " << typing << endl;

   // find first occurrence of a substring - equivalent of strstr()
   string::size_type index = typing.find( "fox" );
   if( index != string::npos )
      cout << "\n\"fox\" first occurs at index " << index;
   else
      cout << "\n\"fox\" is not in the string";
}

Find first out of

#include <iostream>  
  #include <string>  
  using namespace std;  
    
  int main()  {  
     string s1 = "this is a test";  
     int n;  
    
     n = s1.find_first_not_of("aeiouAEIOU");  
     cout << "First consonant at " << n << endl;  
     return 0;  
  }

find last occurrence of a character - equivalent of strrchr()

#include <iostream>
#include <string>
using namespace std;
int main( )
{
   string typing( "The quick, brown fox jumps over the lazy dog" );
   cout << "String:  " << typing << endl;
 
   // find last occurrence of a character - equivalent of strrchr()
   string::size_type index = typing.rfind( "u" );
   if( index != string::npos )
      cout << "\nThe last \"u\" is at index " << index;
   else
      cout << "\nThere is no \"u\" in the string";
}

find last occurrence of a substring - no C-string equivalent

#include <iostream>
#include <string>
using namespace std;
int main( )
{
   string typing( "The quick, brown fox jumps over the lazy dog" );
   cout << "String:  " << typing << endl;
   // find last occurrence of a substring - no C-string equivalent
   string::size_type index = typing.rfind( "fox" );
   if( index != string::npos )
      cout << "\n\"fox\" last occurs at index " << index;
   else
      cout << "\n\"fox\" is not in the string";
}

Find the first occurance

#include <iostream>  
  #include <string>  
  using namespace std;  
    
  int main()  {  
     string s1 = "this is a test";  
     int n;  
    
     n = s1.find_first_of("is");  
     cout << n << endl;  
    
     return 0;  
  }

Find the first of any of these chars

#include <string>
#include <iostream>
int main( ) {
   std::string s = "Search from the beginning";
   std::cout << s.find_first_of("swi")           // Find the first of any of these chars
             << "\n";                            
}
19

Find the first of any of these chars starting from the end

#include <string>
#include <iostream>
int main( ) {
   std::string s = "Search from the beginning";
   std::cout << s.find_last_of("abg") << "\n";   // Find the first of any of these chars starting from the end
}
24

Find the first that"s not in this set

#include <string>
#include <iostream>
int main( ) {
   std::string s = "Search from the beginning";
   std::cout << s.find_first_not_of("the")   // Find the first that"s not in this set
             << "\n";                            // 
}
0

Find the first that"s not in this set, starting from the end

#include <string>
#include <iostream>
int main( ) {
   std::string s = "Search from the beginning";
   std::cout << s.find_last_not_of("from")     // Find the first that"s not in this set, starting from the end
             << "\n";
}
24

search a sub string

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int i;
  string s1 ="this is a test.";
  string s2;
  i = s1.find("is");
  if(i!=string::npos) {
    cout << "Match found at " << i << endl;
    cout << "Remaining string is:\n";
    s2.assign(s1, i, s1.size());
    cout << s2;
  }
  cout << "\n\n";

  return 0;
}
Match found at 2
Remaining string is:
is is a test.

Search from the beginning

#include <string>
#include <iostream>
int main( ) {
   std::string s = "Search from the beginning";
   std::cout << s.find("ar") << "\n";            // Search from the beginning
}
2

Search from the end

#include <string>
#include <iostream>
int main( ) {
   std::string s = "Search from the beginning";
   std::cout << s.rfind("ar") << "\n";           // Search from the end
}
2

Searching for the first occurrence of a letter in a string

#include <iostream>
#include <string>
using namespace std;
void showresult(string s, string::size_type i);
int main(){
  string::size_type indx;
  string str("one two three, one two three");
  string str2;
  cout << "Searching for the first occurrence of t or h\n";
  indx = str.find_first_of("th");
  showresult(str, indx);
  return 0;
}
// Display the results of the search.
void showresult(string s, string::size_type i) {
  if(i == string::npos) {
    cout << "No match found.\n";
    return;
  }
  cout << "Match found at index " << i << endl;
  cout << "Remaining string from point of match: "
       << s.substr(i) << "\n\n";
}

Searching for the first occurrence of any character other than o, n, e, or space

#include <iostream>
#include <string>
using namespace std;
void showresult(string s, string::size_type i);
int main()
{
  string::size_type indx;
  string str("one two three, one two three");
  string str2;
  cout << "Searching for the first occurrence of any character other than o, n, e, or space\n";
  indx = str.find_first_not_of("one ");
  showresult(str, indx);
  return 0;
}
// Display the results of the search.
void showresult(string s, string::size_type i) {
  if(i == string::npos) {
    cout << "No match found.\n";
    return;
  }
  cout << "Match found at index " << i << endl;
  cout << "Remaining string from point of match: "
       << s.substr(i) << "\n\n";
}

Searching for the first occurrence of a substring in a string

#include <iostream>
#include <string>
using namespace std;
void showresult(string s, string::size_type i);
int main()
{
  string::size_type indx;
  string str("one two three, one two three");
  string str2;
  cout << "Searching for the first occurrence of "two"\n";
  indx = str.find("two");
  showresult(str, indx);
  return 0;
}
// Display the results of the search.
void showresult(string s, string::size_type i) {
  if(i == string::npos) {
    cout << "No match found.\n";
    return;
  }
  cout << "Match found at index " << i << endl;
  cout << "Remaining string from point of match: "
       << s.substr(i) << "\n\n";
}

Searching for the last occurrence of a letter in a string

#include <iostream>
#include <string>
using namespace std;
void showresult(string s, string::size_type i);
int main()
{
  string::size_type indx;
  string str("one two three, one two three");
  string str2;
  cout << "Searching for the last occurrence of t or h\n";
  indx = str.find_last_of("th");
  showresult(str, indx);
  return 0;
}
// Display the results of the search.
void showresult(string s, string::size_type i) {
  if(i == string::npos) {
    cout << "No match found.\n";
    return;
  }
  cout << "Match found at index " << i << endl;
  cout << "Remaining string from point of match: "
       << s.substr(i) << "\n\n";
}

Searching for the last occurrence of any character other than o, n, e, or space

#include <iostream>
#include <string>
using namespace std;
void showresult(string s, string::size_type i);
int main()
{
  string::size_type indx;
  string str("one two three, one two three");
  string str2;
  cout << "Searching for the last occurrence of any character other than o, n, e, or space\n";
  indx = str.find_last_not_of("one ");
  showresult(str, indx);
  return 0;
}
// Display the results of the search.
void showresult(string s, string::size_type i) {
  if(i == string::npos) {
    cout << "No match found.\n";
    return;
  }
  cout << "Match found at index " << i << endl;
  cout << "Remaining string from point of match: "
       << s.substr(i) << "\n\n";
}

Searching for the last occurrence of a substring in a string

#include <iostream>
#include <string>
using namespace std;
void showresult(string s, string::size_type i);
int main()
{
  string::size_type indx;
  string str("one two three, one two three");
  string str2;
  cout << "Searching for the last occurrence of "two"\n";
  indx = str.rfind("two");
  showresult(str, indx);
  return 0;
}
// Display the results of the search.
void showresult(string s, string::size_type i) {
  if(i == string::npos) {
    cout << "No match found.\n";
    return;
  }
  cout << "Match found at index " << i << endl;
  cout << "Remaining string from point of match: "
       << s.substr(i) << "\n\n";
}

string.find_first_not_of( substring )

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
   string string1( "This is a test string!");
   int location;
   
   location = string1.find_first_not_of( "is" );
   cout << "\n\n(find_first_not_of) "" << string1[ location ]
      << " " << location;
   return 0;
}
(find_first_not_of) "T 0"

string.find_first_of( substring )

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
   string string1( "This is a test string!");
   int location;
   
   location = string1.find_first_of( "is" );
   cout << "\n\n(find_first_of) found "" << string1[ location ]
      << "at: " << location;
   return 0;
}
(find_first_of) found "iat: 2"

string.find_last_of(substring)

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
   string string1( "This is a test string!");
   int location;
   
   location = string1.find_last_of( "is" );
   cout << "\n\n(find_last_of) found "" << string1[ location ] 
        << " at: " << location;
        
   return 0;
}
(find_last_of) found "i at: 18"

string.find(substring)

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
   string string1( "This is a test string!");
   int location;
   
   cout << "Original string:\n" << string1 
        << "\n\n(find) \"is\" was found at: " << string1.find( "is" ); 
        
   return 0;
}
Original string:
This is a test string!
(find) "is" was found at: 2"

string.rfind(substring)

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
   string string1( "This is a test string!");
   int location;
   
   cout << "Original string:\n" << string1 
        << "\n\n(find) \"is\" was found at: " << string1.rfind( "is" ); 
        
   return 0;
}
Original string:
This is a test string!
(find) "is" was found at: 5"

Using find with reverse iteration

#include <iostream>
#include <vector>
#include <algorithm> // for find
#include <iterator>
using namespace std;

int main()
{
  string s("It is him.");
  vector<char> vector1(s.begin(), s.end());
  ostream_iterator<char> out(cout, " ");

  cout << "chars from the last t to the beginning: ";
  vector<char>::reverse_iterator r = find(vector1.rbegin(), vector1.rend(), "t"
);
  copy(r, vector1.rend(), out); cout << endl;
  cout << "chars from the last t to the end: ";
  copy(r.base() - 1, vector1.end(), out); cout << endl;
  return 0;
}
chars from the last t to the beginning: t I
chars from the last t to the end: t   i s   h i m .