C++/String/string find

Материал из C\C++ эксперт
Перейти к: навигация, поиск

equivalent of strcspn() and strpbrk()

<source lang="cpp">

  1. include <iostream>
  2. include <string>

using namespace std; int main( ) {

  string typing( "The quick, brown fox jumps over the lazy dog" );
  cout << "String:  " << typing << endl;
  // equivalent of strcspn() and strpbrk()
  string::size_type index = typing.find_first_of( "aeiou" );
  if( index != string::npos )
     cout << "\nThe first lower-case vowel is at index " << index;
  else
     cout << "\nThere is no lower-case vowel in the string";

}


 </source>


equivalent of strspn()

<source lang="cpp">

  1. include <iostream>
  2. include <string>

using namespace std; int main( ) {

  string typing( "The quick, brown fox jumps over the lazy dog" );
  cout << "String:  " << typing << endl;
  // equivalent of strspn()
  string::size_type index = typing.find_first_not_of( "aeiou" );
  
  if( index != string::npos )
     cout << "\nThe first letter that is not a lower-case vowel "
        "is at index " << index;
  else
     cout << "\nAll letters in the string are lower-case vowels";

}


 </source>


Find the first of any of these chars

<source lang="cpp">

  1. include <string>
  2. 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

*/        
   
 </source>


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

<source lang="cpp">

  1. include <string>
  2. 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

*/        
   
 </source>


Find the first that"s not in this set

<source lang="cpp">

  1. include <string>
  2. 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

*/        
   
 </source>


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

<source lang="cpp">

  1. include <string>
  2. 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

*/        
   
 </source>


search a sub string

<source lang="cpp">

  1. include <iostream>
  2. 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.

*/        
   
 </source>


Search from the beginning

<source lang="cpp">

  1. include <string>
  2. include <iostream>

int main( ) {

  std::string s = "Search from the beginning";
  std::cout << s.find("ar") << "\n";            // Search from the beginning

} /* 2

*/        
   
 </source>


Search from the end

<source lang="cpp">

  1. include <string>
  2. include <iostream>

int main( ) {

  std::string s = "Search from the beginning";
  std::cout << s.rfind("ar") << "\n";           // Search from the end

} /* 2

*/        
   
 </source>


string.find_first_not_of( substring )

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. 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

*/        
   
 </source>


string.find_first_of( substring )

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. 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

*/        
   
 </source>


string.find_last_of(substring)

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. 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

*/        
   
 </source>


string.find(substring)

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. 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

*/        
   
 </source>


string.rfind(substring)

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. 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

*/        
   
 </source>


Use the STL find() algorithm to obtain an iterator to the start of the first "a"

<source lang="cpp">

  1. include <iostream>
  2. include <string>
  3. include <cctype>
  4. include <algorithm>
  5. include <vector>

using namespace std; int main() {

 string strA("This is a test.");
 string::iterator itr = find(strA.begin(), strA.end(), "a");
 return 0;

}


 </source>


Using find with reverse iteration

<source lang="cpp">

  1. include <iostream>
  2. include <vector>
  3. include <algorithm>
  4. 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 .

*/        
   
 </source>