A
Alexander Dong Back Kim
Dear all,
Sorry about asking simple question again. =)
I'm trying to token a string by a quotation marks. I know this sounds
very simple and easy to implement but what I'm asking here is a better
way of doing it than the following example that I wrote.
// note that each phrase starts with a double-quotation mark and again
ends with a double-quotation mark.
string a = "\"golden apple\" \"cold banana\" \"wine cherry\"";
Since between double-quotation mark("), it can have any special
characters, I realized that I can't token the string by using "
" (space) delimiter. The function (I think it's not really good way of
doing it...) I can think of is the following.
vector<string> token (const string & str)
{
// assume that the str is always valid string against the
quotation rule mentioned above.
bool started = false;
vector<string> result;
string temp;
for (size_t i = 0; i < str.size(); i++)
{
if ( str == '\"')
{
if ( started )
{
// it's time to finish building a string
started = false;
result.push_back (temp);
}
else
{
// it's time to start new string
started = true;
temp.clear();
}
}
else
{
if ( started ) temp += str;
}
}
return result;
}
If I made any mistake above please forgive me because I didn't compile
it but I hopy you understand what I'm trying to do here. Although it
may work well, I think there must be a better way of doing this by
using pure STL classes or functions. And for some reason I feel it's
quite heavy and expensive since it goes through characters one by one.
Thank you =)
Regards,
Alex
Sorry about asking simple question again. =)
I'm trying to token a string by a quotation marks. I know this sounds
very simple and easy to implement but what I'm asking here is a better
way of doing it than the following example that I wrote.
// note that each phrase starts with a double-quotation mark and again
ends with a double-quotation mark.
string a = "\"golden apple\" \"cold banana\" \"wine cherry\"";
Since between double-quotation mark("), it can have any special
characters, I realized that I can't token the string by using "
" (space) delimiter. The function (I think it's not really good way of
doing it...) I can think of is the following.
vector<string> token (const string & str)
{
// assume that the str is always valid string against the
quotation rule mentioned above.
bool started = false;
vector<string> result;
string temp;
for (size_t i = 0; i < str.size(); i++)
{
if ( str == '\"')
{
if ( started )
{
// it's time to finish building a string
started = false;
result.push_back (temp);
}
else
{
// it's time to start new string
started = true;
temp.clear();
}
}
else
{
if ( started ) temp += str;
}
}
return result;
}
If I made any mistake above please forgive me because I didn't compile
it but I hopy you understand what I'm trying to do here. Although it
may work well, I think there must be a better way of doing this by
using pure STL classes or functions. And for some reason I feel it's
quite heavy and expensive since it goes through characters one by one.
Thank you =)
Regards,
Alex