C++C++11C++14C++17Introduction to C++Learn C++

How To Get A Substring Of A String In A C++ App

How To Get A Substring Of A String In A C++ App

How can I use substr() method of std::wstring? How can I get a substring of a string in a C++ app? Here are the answers with C++ examples.

Generally, as an introduction to C++, in addition to int, float, double there is another data type called string that we use for alphanumeric variables. In C++ there are several typedefs for common character types are provided: String types 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 words, string stores the alphanumeric text with 1-byte chars, called ASCII chars. Strings are the instantiation of the basic_string class template that uses char as the character type. Simply we can define a string as below,

string has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can retrieve a substring of a string by using substr() method. Let’s see this method.

How do I get a substring of a string with the substr() method in C++?

The substr() method of std::string, can be used to retrieve a substring of a string. As its parameters, you just need to indicate where to start to retrieve chars (start_position) and the count of chars (char_count). Here is syntax of a substr() method,

Here is a example how we can use substr() method with in this given syntax, see example below,

and the output will be,

Why do I get “Out of Range” when retrieving a substring of a string?

As given in this method we can directly copy, assign, replace or insert any substring of a string. When we use substr() method, we must be sure that the first parameter is lower than its size (s1.size()) and the second parameter is lower than size()-first_parameter, which means both should be inside the string. If second parameter exceeds the string size, it returns substring of string till the end of string without throwing an exception.

for the start_pos=3 the output will be,

in this example above, If the first parameter start_pos is equal to string size it will return empty string, for example for the start_pos=15 the output will be,

and in this example above, if the first parameter exceeds the size of string there will be out_of_range throw, for example for the start_pos=16 the output will be,

If you are checking range carefully you don’t need to use try-catch() method. You can also check these parameters by using its size(). As you see substr() is very easy to use to retrieve any substring of a string, and this substr() method can be used with wstrings as shown in the examples above.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?