Tokening a string

  • Thread starter Alexander Dong Back Kim
  • Start date
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
 
Z

zr

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


You can either use an external library, such as Boost.Regex:
http://www.boost.org/doc/libs/1_38_0/libs/regex/doc/html/index.html
or you can use std::string methods such as find_first_of() and substr
().
 
A

asm23

df said:
You just use the +componentsSeparatedByString method of NSString, which
will return an array of tokens...oh, wait, we're talking about the dying
language of C++. Well, good luck.
Dying language? or You don't like this language?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top