Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Finding Text in a String | Text Data Type
C++ Data Types
course content

Conteúdo do Curso

C++ Data Types

C++ Data Types

1. Introduction
2. Numerical Data Types
3. Text Data Type
4. Other Data Types and Concepts

bookFinding 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:

cpp

main

copy
1234567
#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.

cpp

main

copy
1234567
#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:

cpp

main

copy
12345678
#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 of string.
This number can be accessed with string::npos.

cpp

main

copy
12345678910
#include <iostream> using namespace std; int main() { string str = "codecodefinity"; cout << str.rfind("abc") << endl; cout << string::npos << endl; return 0; }
Tarefa
test

Swipe to show code editor

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 the if statement.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 6
toggle bottom row

bookFinding 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:

cpp

main

copy
1234567
#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.

cpp

main

copy
1234567
#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:

cpp

main

copy
12345678
#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 of string.
This number can be accessed with string::npos.

cpp

main

copy
12345678910
#include <iostream> using namespace std; int main() { string str = "codecodefinity"; cout << str.rfind("abc") << endl; cout << string::npos << endl; return 0; }
Tarefa
test

Swipe to show code editor

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 the if statement.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 6
toggle bottom row

bookFinding 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:

cpp

main

copy
1234567
#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.

cpp

main

copy
1234567
#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:

cpp

main

copy
12345678
#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 of string.
This number can be accessed with string::npos.

cpp

main

copy
12345678910
#include <iostream> using namespace std; int main() { string str = "codecodefinity"; cout << str.rfind("abc") << endl; cout << string::npos << endl; return 0; }
Tarefa
test

Swipe to show code editor

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 the if statement.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu 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:

cpp

main

copy
1234567
#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.

cpp

main

copy
1234567
#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:

cpp

main

copy
12345678
#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 of string.
This number can be accessed with string::npos.

cpp

main

copy
12345678910
#include <iostream> using namespace std; int main() { string str = "codecodefinity"; cout << str.rfind("abc") << endl; cout << string::npos << endl; return 0; }
Tarefa
test

Swipe to show code editor

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 the if statement.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 3. Capítulo 6
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt