[Solved] How can I divide std::string to 2 std::strings? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[Solved] How can I divide std::string to 2 std::strings?

I need to divide std::string to 2 variables. The first variable (operation) is the first symbol of the big string. The second (area) is the string from the third symbol to the end. For example: # 3383838 area = 3383838; operation = '#' How can I do it?

4th Nov 2021, 7:42 AM
Kind_Cat
Kind_Cat - avatar
7 Answers
+ 2
Is # the only symbol used as operation mark? Is there possibility that a string contains multiple operations and areas?
4th Nov 2021, 7:47 AM
Ipang
+ 1
No, there is only 1 area and 1 operation in one request, but there is 2 different operations. (# and %)
4th Nov 2021, 7:50 AM
Kind_Cat
Kind_Cat - avatar
+ 1
#include <string> #include <iostream> int main() { std::string sample { "# 33883838" }; // https://en.cppreference.com/w/cpp/string/basic_string/front std::string operation { sample.front() }; // https://en.cppreference.com/w/cpp/string/basic_string/substr std::string area { sample.substr( 2 ) }; std::cout << operation << "\n" << area << "\n"; return 0; }
4th Nov 2021, 9:31 AM
Ipang
+ 1
So, I can write this: operation = request.substr(0, 1) area = request.substr(2). Am I right
4th Nov 2021, 9:35 AM
Kind_Cat
Kind_Cat - avatar
+ 1
Yeah, that'll do it 👌
4th Nov 2021, 10:04 AM
Ipang
+ 1
Thanks
4th Nov 2021, 10:05 AM
Kind_Cat
Kind_Cat - avatar
+ 1
Okay 👍
4th Nov 2021, 10:06 AM
Ipang