Support Online
Skip to main content

C++ std::string::find() Usage

What Will You Learn in This Guide?

In this guide, you will learn the std::string::find() method, which is used to search for subtext within a text in C++.
You'll also see step-by-step instructions on how to use the constant std::string::npos and specify character limits for C-style text.

Technical Summary

The find() method returns the first occurrence of a substring within another text.
If the subtext is not found, a special constant named std::string::npos is returned.

Syntax:

size_t find(const std::string& aranan, size_t pos = 0);

searched → Subtext you want to search for

pos (optional) → Index from which to start the search (default 0)

Return → Index if found, std::string::npos if not found



#include <iostream>
#include <string>
using namespace std;

int main() {
string ana_metin = "tr1-node01 ornek.com";
string hedef = "genixnode";

size_t konum = ana_metin.find(hedef);

if (konum != string::npos)
cout << "Bulundu! Baslangic indeksi: " << konum << endl;
else
cout << "Alt metin bulunamadi." << endl;

return 0;
}
  • This example returns the first position where the subtext "genixnode" is found within the main text.

Specifying the Search Start (pos Parameter)


#include <iostream>
#include <string>
using namespace std;

int main() {
string metin = "GenixNode hizmeti 2024 Mart'ta yayinlandi.";
string kelime = "Mart";

size_t konum = metin.find(kelime, 10); // 10. karakterden itibaren ara

if (konum != string::npos)
cout << "Bulundu! Konum: " << konum << endl;
else
cout << "Belirtilen aralikta bulunamadi." << endl;

return 0;
}

The > pos parameter is used to start the search from the middle of the text. However, the returned index value is calculated from the beginning of the text.


Secure Control: std::string::npos

npos is the special constant returned when the search fails. This value is the maximum value of type size_t and means “not found”.


#include <iostream>
#include <string>
using namespace std;

int main() {
string veri = "API baglanti basarili";
string hata = "HATA";

if (veri.find(hata) != string::npos)
cout << "Hata metni bulundu!" << endl;
else
cout << "Her sey yolunda." << endl;

return 0;
}
  • Always compare the find() result with npos. Otherwise you may be working with an invalid location.

Character Limit for C-Style Texts (count Parameter)


#include <iostream>
#include <string>
using namespace std;

int main() {
string metin = "GenixNode Hesap";

size_t konum = metin.find("GenixNode Deneme", 0, 6); // sadece ilk 6 karakter: "GenixNode"

if (konum != string::npos)
cout << "Eşleşme bulundu. Konum: " << konum << endl;
else
cout << "Eşleşme bulunamadi." << endl;

return 0;
}
  • This version is only valid for C-style arrays (char)*. The count parameter determines how many characters the search will be limited to.

Summary Table: Using find()

Usage MethodParametersDescription
find(str)Alt textSearches the entire text
find(str, pos)Alt text + starting positionSearches from the specified location
find(char*, pos, count)C-style text + borderSearches the specified length
std::string::nposReturns when not found

Frequently Asked Questions (FAQ)

  1. Is find() case sensitive?

Yes. "API" and "api" are considered different. Tolower() or transform() can be used for case-insensitive searching.

  1. How do I find all matches?

Call find() again in a loop:


pos = metin.find(kelime, pos + 1);
  1. What does find() return?

If there is a match, its position (size_t) is returned, otherwise std::string::npos is returned.

  1. What is the difference between rfind()?

rfind() searches backwards and returns the location of the last match.

--

Result

In C++, the std::string::find() method is one of the basic building blocks of text search operations. Always check the result with std::string::npos and, if necessary, search from the end with rfind().

You can try these examples in your own virtual development environment on GenixNode and strengthen your C++ text processing skills.