How can I find a string inside another string? What kind of methods I can use in a C++ app to find a string in a std::string? How can I use find() methods with strings?
Modern C++ uses Strings, Wide Strings, and Unicode Strings to support worldwide languages. Strings (std::string) uses char as the character type which means they are ASCII chars and they are an instantiation of the basic_string class template. In C++, there are several typedefs for common character types and one of them is std::string types that are defined in header <string>.
strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In other terms, string stores the alphanumeric text with 1-byte chars called char. Strings are the instantiation of the basic_string class template that uses char as the character type. In modern C++, simply we can define a string as below,
1 2 3 |
std::string str = "This is a String"; |
Strings (string) are a class contains arrays of characters with useful methods, and we can access, or modify their characters easily. In C++, while string contents are defined between ” and “, characters are defined between ‘ and ‘.
The string has methods to append, assign, copy, align, replace or operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can find a string in another string with find() method..
Table of Contents
How do I use the find() Method of string in a C++ app?
The find() method is a String Method that finds a string in its string . There can be a position to start as a second parameter, search begins from this position
to the end of string. find() method returns the position of the first character of the found substring. If no such substring is found in that string then it returns npos of the string (std::string::npos).
Syntax:
1 2 3 |
size_type find( const basic_string& str) const; |
How to search a string in C++ with the find() Method
Simply we can find a string in a string by using its find method as given example below.
1 2 3 4 5 6 7 |
std::string str = "This is a String"; int result = str.find( "String" ); // searching "String" in string str if( result == std::string::npos ) std::cout << "not found string\n"; else std::cout << "found string\n"; |
Is there a full C++ example of using the find() method of a string?
This full C++ example below shows how we can find a string in another string by using find() method,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <string> int main() { std::string str = "This String is a String"; std::cout << str << std::endl; int pos = str.find("String"); // Searching a string in string str if( pos != std::string::npos ) { std::cout << "String found at " << pos << std::endl; } else { std::cout << "String not found" << std::endl; } getchar(); return 0; } |
Output of this code will print the original string and prints the position if it finds the string as below,
1 2 3 4 |
This String is a String String found at 5 |
as you see in this example we found the first “String” that is at the position 5 which means starts from the 6th character ‘S’ of this string.
How to search a string from a given position of a string?
The find() method is a String Method that finds a string in its string . There can be a position to start as a second parameter, search begins from this position
to the end of string. find() method returns the position of the first character of the found substring. If no such substring is found in that string then it returns npos of the string (std::string::npos).
Syntax:
1 2 3 |
size_type find( const basic_string& str, size_type position) const; |
This full C++ example below shows how we can find a string in another string by using find() method,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <string> int main() { std::string str = "This String is a String"; std::cout << str << std::endl; int pos = str.find( "String", 6 ); // Searching a string from position 6 in string str if( pos != std::string::npos ) { std::cout << "String found at " << pos << std::endl; } else { std::cout << "String not found" << std::endl; } getchar(); return 0; } |
Output of this code will print the original string and prints the position if it finds the string as below,
1 2 3 4 |
This String is a String String found at 17 |
as you see in this example we found the second “String” by using search start position at 6. Result is at the position 17 which means starts from the 18th character ‘S’ of this string.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition