The strContains function returns true or false depending on whether the input string contains the search string. If you have ever used the contains function in C# or VB then you may know how useful it can be.
bool strContains(const string inputStr, const string searchStr)
{
size_t contains;
contains = inputStr.find(searchStr);
if(contains != string::npos)
return true;
else
return false;
}
/* Example */
#include <iostream>
#include <string>
using namespace std;
bool strContains(const string inputStr, const string searchStr)
{
size_t contains;
contains = inputStr.find(searchStr);
if(contains != string::npos)
return true;
else
return false;
}
int main()
{
string sentence = "Hello World, This is a test of the contains function";
if(strContains(sentence, "contains function"))
{
cout << "It works!" << endl;
}
else
cout << "Oops, time to rethink" << endl;
return 0;
}
/* End of example */
Artikel Terkait:
Algoritma dan Pemrograman
- Class Inheritance
- OOP Recursive Decent Parsing III
- OOP Recursive Decent Parsing II
- OOP Recursive Decent Parsing I
- Calculate Sequebce by Using Iterative Function
- A Comparison of Sorting Algorithms
- Typical data sequences for testing sorting algorithms
- Quicksort Algorithm
- Vector
- Function to reverse an integer array
- Array
- RECCURENCE RELATION
- Recursion
- Recursion Shortest Function to Reverse a String
- Calculator (C++)
- Recursive Format numbers with commas
- Calling functions with Inline Assembly
- Recursive Function
- Algoritma Menentukan Tahun Kabisat
- Algoritma dan Class Bilangan Prima
- Class Luas Lingkaran dan Volume Bola With Jeliot
- Class Bilangan [Bahasa C++]
- Struktur Perulangan (Lanjutan)
- Struktur Perulangan
Comments :
0 comments to “String Contains function”
Post a Comment