简体   繁体   中英

C++ Extracting the integer in a string with multiple delimiters

I am trying to extract the integers from a string. What could be wrong here? I only get the first value. How can I get it working even with zero's in the string?

string str="91,43,3,23,0;6,9,0-4,29,24";
std::stringstream ss(str);
int x;
while(ss >> x)
{
    cout<<"GOT->"<<x<<endl;
    char c;
    ss >> c; //Discard a non space char.
    if(c != ',' || c != '-' || c != ';')
    {
        ss.unget();
    }
}

Look very closely at this line:

if(c != ',' || c != '-' || c != ';')

Note that this condition is always true, so you are always unget ing the punctuation character. The next read will then always fail as it reads punctuation when a number is expected. Changing the || 's to && 's should fix the problem.

Of course, your code assumes that str is formatted in a very particular way and might break when given a differently-formatted str value. Just be aware of that.

u can get this done with boost split.

   int main() {
      std::stringstream ss;
      std::string inputString = "91,43,3,23,0;6,9,0-4,29,24";
      std::string delimiters("|,:-;");
      std::vector<std::string> parts;
      boost::split(parts, inputString, boost::is_any_of(delimiters));
      for(int i = 0; i<parts.size();i++ ) {
           std::cout <<parts[i] << " ";
      }
   return 0;
   }

Output (Just integers) :- 91 43 3 23 0 6 9 0 4 29 24

This will change the string into char and write off : , ; -

#include <iostream>
#include <string>
using namespace std;
int main(){ 
    string str = "91,43,3,23,0;6,9,0-4,29,24";
    str.c_str();  // ex: string a; --> char a[];
    char a[99];
    int j = 0;
    int x;
    for(int i = 0; i < str.length(); i++){
        if (str[i]!=',' && str[i]!=';' && str[i]!='-'){
            a[j] = str[i];
            j++;
        }
    }
    return 0;
}

Hope this will help you.

This suits my purpose where in I can extract the integers and also add the delimiters if necessary. Works with different formatted strings as well. (I dont have boost lib, hence preferring this method. )

int main()
{
   string  str="2,3,4;0,1,3-4,289,24,21,45;2";
   //string  str=";2;0,1,3-4,289,24;21,45;2"; //input2

   std::stringstream ss(str);
   int x=0;

   if( str.length() != 0 )
   {
      while( !ss.eof() )
      {
         if( ss.peek()!= ',' && ss.peek()!=';' && ss.peek()!='-') /*Delimiters*/
         {
            ss>>x;
            cout<<"val="<<x<<endl;
             /* TODO:store integers do processing */
         }
         ss.get();
      }
   }
}

You can also try:

vector<int> SplitNumbersFromString(const string& input, const vector<char>& delimiters)
{
    string buff{""};
    vector<int> output;

    for (auto n : input)
    {
        if (none_of(delimiters.begin(), delimiters.end(), [n](const char& c){ return c == n; }))
        {
            buff += n;
        }
        else
        {
            if (buff != "")
            {
                output.push_back(stoi(buff));
                buff = "";
            }
        }
    }
    if (buff != "") output.push_back(stoi(buff));

    return output;
}

vector<char> delimiters = { ',', '-', ';' };
vector<int> numbers = SplitNumbersFromString("91,43,3,23,0;6,9,0-4,29,24", delimiters);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM