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

Зміст курсу

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

Method find()

Sometimes, you may need to search for specific text within a string. This can be done using the .find() or .rfind() methods. Let’s begin with the .find() method.

h

find

copy
1
str.find("text to find")

It returns the index of the first character of the first match.

cpp

main

copy
1234567
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; // (code)codefinity }

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.find("code", 3) << std::endl; // __de(code)finity }

Method rfind()

You can also locate the last occurrence of some text using the .rfind() method.

h

rfind

copy
1
str.rfind("text to find")

While .find() retrieves the first occurrence of text, .rfind() finds the last occurrence and stands for reverse find.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; std::cout << str.rfind("code") << std::endl; }

Note

If no match is found, both .find() and .rfind() return a special value, string::npos. This value represents no position and indicates that the search was unsuccessful.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << endl; std::cout << std::string::npos << endl; }
Завдання
test

Swipe to show code editor

Create a program that prints "Found code" if the string contains "code", and "No code" if it doesn’t.

  1. Write a condition to check for no match using .find() or .rfind(). Refer to the hint if needed.
  2. Replace the "___" placeholders with "Found code" or "No code", based on your if statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 6
toggle bottom row

bookFinding Text in a String

Method find()

Sometimes, you may need to search for specific text within a string. This can be done using the .find() or .rfind() methods. Let’s begin with the .find() method.

h

find

copy
1
str.find("text to find")

It returns the index of the first character of the first match.

cpp

main

copy
1234567
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; // (code)codefinity }

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.find("code", 3) << std::endl; // __de(code)finity }

Method rfind()

You can also locate the last occurrence of some text using the .rfind() method.

h

rfind

copy
1
str.rfind("text to find")

While .find() retrieves the first occurrence of text, .rfind() finds the last occurrence and stands for reverse find.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; std::cout << str.rfind("code") << std::endl; }

Note

If no match is found, both .find() and .rfind() return a special value, string::npos. This value represents no position and indicates that the search was unsuccessful.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << endl; std::cout << std::string::npos << endl; }
Завдання
test

Swipe to show code editor

Create a program that prints "Found code" if the string contains "code", and "No code" if it doesn’t.

  1. Write a condition to check for no match using .find() or .rfind(). Refer to the hint if needed.
  2. Replace the "___" placeholders with "Found code" or "No code", based on your if statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 6
toggle bottom row

bookFinding Text in a String

Method find()

Sometimes, you may need to search for specific text within a string. This can be done using the .find() or .rfind() methods. Let’s begin with the .find() method.

h

find

copy
1
str.find("text to find")

It returns the index of the first character of the first match.

cpp

main

copy
1234567
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; // (code)codefinity }

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.find("code", 3) << std::endl; // __de(code)finity }

Method rfind()

You can also locate the last occurrence of some text using the .rfind() method.

h

rfind

copy
1
str.rfind("text to find")

While .find() retrieves the first occurrence of text, .rfind() finds the last occurrence and stands for reverse find.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; std::cout << str.rfind("code") << std::endl; }

Note

If no match is found, both .find() and .rfind() return a special value, string::npos. This value represents no position and indicates that the search was unsuccessful.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << endl; std::cout << std::string::npos << endl; }
Завдання
test

Swipe to show code editor

Create a program that prints "Found code" if the string contains "code", and "No code" if it doesn’t.

  1. Write a condition to check for no match using .find() or .rfind(). Refer to the hint if needed.
  2. Replace the "___" placeholders with "Found code" or "No code", based on your if statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Method find()

Sometimes, you may need to search for specific text within a string. This can be done using the .find() or .rfind() methods. Let’s begin with the .find() method.

h

find

copy
1
str.find("text to find")

It returns the index of the first character of the first match.

cpp

main

copy
1234567
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; // (code)codefinity }

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.find("code", 3) << std::endl; // __de(code)finity }

Method rfind()

You can also locate the last occurrence of some text using the .rfind() method.

h

rfind

copy
1
str.rfind("text to find")

While .find() retrieves the first occurrence of text, .rfind() finds the last occurrence and stands for reverse find.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; std::cout << str.rfind("code") << std::endl; }

Note

If no match is found, both .find() and .rfind() return a special value, string::npos. This value represents no position and indicates that the search was unsuccessful.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << endl; std::cout << std::string::npos << endl; }
Завдання
test

Swipe to show code editor

Create a program that prints "Found code" if the string contains "code", and "No code" if it doesn’t.

  1. Write a condition to check for no match using .find() or .rfind(). Refer to the hint if needed.
  2. Replace the "___" placeholders with "Found code" or "No code", based on your if statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 6
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt