+ 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?
8 Respuestas
+ 2
Is # the only symbol used as operation mark?
Is there possibility that a string contains multiple operations and areas?
+ 1
No, there is only 1 area and 1 operation in one request, but there is 2 different operations. (# and %)
+ 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;
}
+ 1
So, I can write this:
operation = request.substr(0, 1)
area = request.substr(2).
Am I right
+ 1
Yeah, that'll do it 👌
+ 1
Thanks
+ 1
Okay 👍