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

Finding Text in a StringFinding 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.cpp

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

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

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

Tarefa

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.

Tudo estava claro?

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

Conteúdo do Curso

C++ Data Types

Finding Text in a StringFinding 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.cpp

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

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

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

Tarefa

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.

Tudo estava claro?

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