• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

remove quotes from a char*

Hi

I have a char* which has quotes in it like

cons char* adlist = \" iphone, windows \";

my goal is to tokenize by commas and extract  iphone and windows  nand compare them indivually.

my code looks liem following

bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false;
}

the problem is , when the string is tokenized the quotes are copied as well

like "iphone and
windows"

which i want to compare with teh value of client os which is iphone or windows

how do i best remove teh quotes?

i can use

 adlist.erase(remove(adlist.begin(), adlist.end(), '\"' ),adlist.end());

but that would need conversion of const ckar* to a new string ...whats the most efficient way?





bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window

0
Vlearns
Asked:
Vlearns
  • 10
  • 4
1 Solution
 
VlearnsAuthor Commented:
      adlist++;
       adlist[strlen(adlist) - 1] = '\0';

is this the best solution?
0
 
jkrCommented:
Well, you have 'no_ad_partner' as a std::string, why not removing the quotes there? E.g.
bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {

               no_ad_partner.erase(remove(no_ad_partner.begin(), no_ad_partner.end(), '\"' ),no_ad_partner.end());

               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window


Alternatively, you could remove them from 'temp':
bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       temp.str().erase(remove(temp.str().begin(), temp.str().end(), '\"' ),temp.str().end());

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window

0
 
VlearnsAuthor Commented:
if there are no quotes then the operation would still be safe , right?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
VlearnsAuthor Commented:
one more question, say

adlist was not a comma separated string but just a single token...like ...."iphone" or iphone...

this code would still work right?
0
 
VlearnsAuthor Commented:
somethings not quite right...

i am seeing

unexpected std::exception (basic_string::_S_create); re-throw...


in the logs and the code is exiting

0
 
VlearnsAuthor Commented:
pasting my code...is there anything wrong?
bool process_adlist(char* adlist, yutString cos)
{

   if (adlist && cos) {
       stringstream temp(adlist);
       temp.str().erase(remove(temp.str().begin(), temp.str().end(), '\"' ),temp.str().end());
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false;
}

Open in new window

0
 
jkrCommented:
Sure - yet I just noticed that you cannot use 'erase()' the way you described it, so that should be
void remove_quotes(string& s) {

  int pos = 0;

  while(string::npos != (npos = s.find('\"',pos)) s.erase(pos);
}

bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {

               remove_quotes(no_ad_partner;)

               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window


and/or
void remove_quotes(string& s) {

  int pos = 0;

  while(string::npos != (npos = s.find('\"',pos)) s.erase(pos);
}

bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       remove_quotes(temp.str());

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window

0
 
VlearnsAuthor Commented:
thanks!

is there a way to do this without having to write a new function?
0
 
VlearnsAuthor Commented:
tried bool UserIMAP::process_adlist(char* adlist, yutString cos)
{
   int pos = 0;
   if (adlist && cos.c_str()) {
       stringstream temp(adlist);
       while(string::npos != (npos = temp.str().find('\"',pos))) temp.str().erase(pos);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false;
}    

getting  error: `npos' was not declared in this scope
warning: unused variable 'npos'

0
 
jkrCommented:
Srry, that was a typo on my side, it should be
void remove_quotes(string& s) {

  int pos = 0;

  while(string::npos != (pos = s.find('\"',pos)) s.erase(pos);
}

Open in new window


or
bool UserIMAP::process_adlist(char* adlist, yutString cos)
{
   int pos = 0;
   if (adlist && cos.c_str()) {
       stringstream temp(adlist);
       while(string::npos != (pos = temp.str().find('\"',pos))) temp.str().erase(pos);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false;
}

Open in new window

0
 
VlearnsAuthor Commented:
i am geting a compiler error

 warning: comparison between signed and unsigned integer expressions

0
 
VlearnsAuthor Commented:
changed to
uint pos = 0;
0
 
jkrCommented:
Well, I should probably not post code while being on the phone ;o)

I hope it works now...
0
 
VlearnsAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 10
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now