Reputation: 11431
I have a requirement like this: I have a string like "-myArg:ArgVal".
std::string strArg = "-myArg:ArgVal";
Now, I have to check in above string first character is always '-' and if first character is '-' i should remove it and i should store "myArg" and "ArgVal" in two different string objects.
How can I do this efficiently?
Upvotes: 0
Views: 235
Reputation: 4110
std::string has the functions you need. You can check the first char by using string::at
and create substrings with string::substr
. Erasing single chars works the same comfortable way.
See a c++ reference for more information.
Upvotes: 0
Reputation: 11541
The most scalable and solid way is via regular expressions. Recommended library is Boost.Regex
Upvotes: 1
Reputation: 46607
Have a look at std::string::substr()
and std::string::find()
.
Upvotes: 1
Reputation: 35256
Try this
if (strArg[0] == '-') {
strVar1 = strArg.substr(1, strArg.find(':') - 1);
strVar2 = strArg.substr(strArg.find(':') + 1);
}
Of course I'm assuming that if the string starts with '-'
then there will be a ':'
in it with chars before and after. You should probably check this because if there isn't it can cause an error
Upvotes: 3