Is there any way to read a formatted string like this, for example :48754+7812=Abcs
.
Let's say I have three stringz X,Y and Z, and I want
X = 48754
Y = 7812
Z = Abcs
The size of the two numbers and the length of the string may vary, so I dont want to use substring()
or anything like that.
Is it possible to give C++ a parameter like this
":#####..+####..=SSS.."
so it knows directly what's going on?
for example.
and second variant, work only with string.
You can use
scanf
. It is not overly C++ - ish, but it does the trick with remarkably few lines of code:The idea is to specify the characters accepted by the strings that you read using a very limited regular expression syntax. In this case, the first string is read up to the plus, and the second string is read up to the equals sign.
A possibility is
boost::split()
, which allows the specification of multiple delimiters and does not require prior knowledge of the size of the input:Or, using
sscanf()
:However, the limitiation here is that the maximum length of the string (
Z
) must be decided before parsing the input.