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,
1 2 3 |
std::wstring wstr = L"This is a Wide String"; |
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.
In this post, you’ll learn how to use the std::wstring substr() method and how to get a substring of a wide string. By learning these substrings of a wide string in c++, it will help you build C++ applications with the use of C++ IDE.
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,
1 2 3 |
basic_string substr( size_type start_position, size_type char_count) const; |
Here is a example how we can use substr() method with in this given syntax, see example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::wstring s1, s2; s1 = L"my first wide string"; s2 = s1.substr(3,5); std::wcout << "String: '" << s1 << "'\n"; std::wcout << "Sub String: '" << s2 << "'\n"; getchar(); return 0; } |
as you see we use wcout to printout wide strings and the output will be,
1 2 3 4 |
String: 'my first wide string' Sub String: 'first' |
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.
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 26 27 28 29 30 |
#include <iostream> #include <string> int main() { std::wstring s1, s2; s1 = L"my first wide string"; int start_pos = 3; try { std::wstring s2 = s1.substr( start_pos, 5); std::wcout << "String: '" << s1 << "'\n"; std::wcout << "Sub String: '" << s2 << "'\n"; } catch(const std::out_of_range& e) { std::wcout << "out of range in substring, please check string size\n"; } getchar(); return 0; } |
for the start_pos=3 the output will be,
1 2 3 4 |
String: 'my first wide string' Sub String: 'first' |
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,
1 2 3 4 |
String: 'my first wide string' Sub String: '' |
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,
1 2 3 |
out of range in substring, please check string size |
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.