Course Content
C++ Data Types
C++ Data Types
Finding Text in a String
In some cases, you may want to look for specific text in your string
. It can be done using .find()
or.rfind()
method. Let's start with the .find()
method. Here is the syntax:
It returns the index of the first character of the first match. Let's look at the example:
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code", 3) << std::endl; // __de(code)finity }
You can also specify the position of the first character in the string to be considered in the search.
It can be done using the pos
argument. Any characters before the pos
index are ignored in a search.
Here is an example of finding the first "code"
starting from a 3-rd character.
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("code") << std::endl; // code(code)finity }
The .find(str2)
method finds the first occurrence of str2
.
Alternatively you can find the last character of the last occurrence of str2
using the
.rfind(str2)
method. Here is the syntax of .rfind()
:
Here is an example:
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << std::endl; std::cout << std::string::npos << std::endl; }
So .rfind()
is a reversed find.
Instead of finding the first occurrence, it finds the last one.
And instead of returning the first character of occurrence, it returns the last.
Note
If there is no match, both
.find()
and.rfind()
return some weird number,npos
ofstring
.
This number can be accessed withstring::npos
.
main
#include <iostream> using namespace std; int main() { string str = "codecodefinity"; cout << str.rfind("abc") << endl; cout << string::npos << endl; return 0; }
Task
Build a program that outputs "Found code"
if "code"
is in a string, and "No code"
if "code"
is not in a string.
- Write a statement to check if there is no match using
.find()
or.rfind()
. If you have problems with this step, check out the hint. - Fill the
"___"
gaps with"Found code"
or"No code"
, depending on how you wrote theif
statement.
Thanks for your feedback!
Finding Text in a String
In some cases, you may want to look for specific text in your string
. It can be done using .find()
or.rfind()
method. Let's start with the .find()
method. Here is the syntax:
It returns the index of the first character of the first match. Let's look at the example:
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code", 3) << std::endl; // __de(code)finity }
You can also specify the position of the first character in the string to be considered in the search.
It can be done using the pos
argument. Any characters before the pos
index are ignored in a search.
Here is an example of finding the first "code"
starting from a 3-rd character.
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("code") << std::endl; // code(code)finity }
The .find(str2)
method finds the first occurrence of str2
.
Alternatively you can find the last character of the last occurrence of str2
using the
.rfind(str2)
method. Here is the syntax of .rfind()
:
Here is an example:
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << std::endl; std::cout << std::string::npos << std::endl; }
So .rfind()
is a reversed find.
Instead of finding the first occurrence, it finds the last one.
And instead of returning the first character of occurrence, it returns the last.
Note
If there is no match, both
.find()
and.rfind()
return some weird number,npos
ofstring
.
This number can be accessed withstring::npos
.
main
#include <iostream> using namespace std; int main() { string str = "codecodefinity"; cout << str.rfind("abc") << endl; cout << string::npos << endl; return 0; }
Task
Build a program that outputs "Found code"
if "code"
is in a string, and "No code"
if "code"
is not in a string.
- Write a statement to check if there is no match using
.find()
or.rfind()
. If you have problems with this step, check out the hint. - Fill the
"___"
gaps with"Found code"
or"No code"
, depending on how you wrote theif
statement.
Thanks for your feedback!
Finding Text in a String
In some cases, you may want to look for specific text in your string
. It can be done using .find()
or.rfind()
method. Let's start with the .find()
method. Here is the syntax:
It returns the index of the first character of the first match. Let's look at the example:
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code", 3) << std::endl; // __de(code)finity }
You can also specify the position of the first character in the string to be considered in the search.
It can be done using the pos
argument. Any characters before the pos
index are ignored in a search.
Here is an example of finding the first "code"
starting from a 3-rd character.
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("code") << std::endl; // code(code)finity }
The .find(str2)
method finds the first occurrence of str2
.
Alternatively you can find the last character of the last occurrence of str2
using the
.rfind(str2)
method. Here is the syntax of .rfind()
:
Here is an example:
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << std::endl; std::cout << std::string::npos << std::endl; }
So .rfind()
is a reversed find.
Instead of finding the first occurrence, it finds the last one.
And instead of returning the first character of occurrence, it returns the last.
Note
If there is no match, both
.find()
and.rfind()
return some weird number,npos
ofstring
.
This number can be accessed withstring::npos
.
main
#include <iostream> using namespace std; int main() { string str = "codecodefinity"; cout << str.rfind("abc") << endl; cout << string::npos << endl; return 0; }
Task
Build a program that outputs "Found code"
if "code"
is in a string, and "No code"
if "code"
is not in a string.
- Write a statement to check if there is no match using
.find()
or.rfind()
. If you have problems with this step, check out the hint. - Fill the
"___"
gaps with"Found code"
or"No code"
, depending on how you wrote theif
statement.
Thanks for your feedback!
In some cases, you may want to look for specific text in your string
. It can be done using .find()
or.rfind()
method. Let's start with the .find()
method. Here is the syntax:
It returns the index of the first character of the first match. Let's look at the example:
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code", 3) << std::endl; // __de(code)finity }
You can also specify the position of the first character in the string to be considered in the search.
It can be done using the pos
argument. Any characters before the pos
index are ignored in a search.
Here is an example of finding the first "code"
starting from a 3-rd character.
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("code") << std::endl; // code(code)finity }
The .find(str2)
method finds the first occurrence of str2
.
Alternatively you can find the last character of the last occurrence of str2
using the
.rfind(str2)
method. Here is the syntax of .rfind()
:
Here is an example:
main
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << std::endl; std::cout << std::string::npos << std::endl; }
So .rfind()
is a reversed find.
Instead of finding the first occurrence, it finds the last one.
And instead of returning the first character of occurrence, it returns the last.
Note
If there is no match, both
.find()
and.rfind()
return some weird number,npos
ofstring
.
This number can be accessed withstring::npos
.
main
#include <iostream> using namespace std; int main() { string str = "codecodefinity"; cout << str.rfind("abc") << endl; cout << string::npos << endl; return 0; }
Task
Build a program that outputs "Found code"
if "code"
is in a string, and "No code"
if "code"
is not in a string.
- Write a statement to check if there is no match using
.find()
or.rfind()
. If you have problems with this step, check out the hint. - Fill the
"___"
gaps with"Found code"
or"No code"
, depending on how you wrote theif
statement.