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

This Is How To Get A Substring of a Wide String in C++

How can I use substr() method of std::wstring? How can I get a substring of a wide string? 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 wstring that we use for alphanumeric variables. In C++ there are several typedefs of common character types are provided: String types are defined in header <string>.

wstrings are the string class for byte characters represented with wstring and alphanumeric characters are stored and displayed in string forms. In other terms wstring stores for the alphanumeric text with 2-byte chars, called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. Simply we can define a string as below,

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

You can retrieve a substring of a String with the substr() Method

substr() method of std::wstring, can be used to retrieve a substring of a wide 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,

as you see we use wcout to printout wide strings and the output will be,

What does it mean if you get “Out of Range” when retrieving a Substring of a String in C++?

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 the second parameter exceeds the string size, it returns a substring of wide string till the end of the wide 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 = 20 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 = 21 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 retrieve any substring of a string, and this substr() method can be used with wstrings as given examples above.


You can download a free trial of C++ Builder and discover how easy it is to use to create powerful modern programs. Click here to try it for free yourself.

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

C++Introduction to C++Language FeatureLearn C++

How to Create a New Windows FMX Static Library In C++

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

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

ru_RURussian