Parse (split) a string in C++ using string delimiter (standard C++)

后端 未结 20 2440
时光说笑
时光说笑 2020-11-21 23:44

I am parsing a string in C++ using the following:

using namespace std;

string parsed,input=\"text to be parsed\";
stringstream input_stringstream(input);

i         


        
20条回答
  •  孤城傲影
    2020-11-22 00:10

    Since this is the top-rated Stack Overflow Google search result for C++ split string or similar, I'll post a complete, copy/paste runnable example that shows both methods.

    splitString uses stringstream (probably the better and easier option in most cases)

    splitString2 uses find and substr (a more manual approach)

    // SplitString.cpp
    
    #include 
    #include 
    #include 
    #include 
    
    // function prototypes
    std::vector splitString(const std::string& str, char delim);
    std::vector splitString2(const std::string& str, char delim);
    std::string getSubstring(const std::string& str, int leftIdx, int rightIdx);
    
    
    int main(void)
    {
      // Test cases - all will pass
      
      std::string str = "ab,cd,ef";
      //std::string str = "abcdef";
      //std::string str = "";
      //std::string str = ",cd,ef";
      //std::string str = "ab,cd,";   // behavior of splitString and splitString2 is different for this final case only, if this case matters to you choose which one you need as applicable
      
      
      std::vector tokens = splitString(str, ',');
      
      std::cout << "tokens: " << "\n";
      
      if (tokens.empty())
      {
        std::cout << "(tokens is empty)" << "\n";
      }
      else
      {
        for (auto& token : tokens)
        {
          if (token == "") std::cout << "(empty string)" << "\n";
          else std::cout << token << "\n";
        }
      }
        
      return 0;
    }
    
    std::vector splitString(const std::string& str, char delim)
    {
      std::vector tokens;
      
      if (str == "") return tokens;
      
      std::string currentToken;
      
      std::stringstream ss(str);
      
      while (std::getline(ss, currentToken, delim))
      {
        tokens.push_back(currentToken);
      }
      
      return tokens;
    }
    
    std::vector splitString2(const std::string& str, char delim)
    {
      std::vector tokens;
      
      if (str == "") return tokens;
      
      int leftIdx = 0;
      
      int delimIdx = str.find(delim);
      
      int rightIdx;
      
      while (delimIdx != std::string::npos)
      {
        rightIdx = delimIdx - 1;
        
        std::string token = getSubstring(str, leftIdx, rightIdx);
        tokens.push_back(token);
        
        // prep for next time around
        leftIdx = delimIdx + 1;
        
        delimIdx = str.find(delim, delimIdx + 1);
      }
      
      rightIdx = str.size() - 1;
      
      std::string token = getSubstring(str, leftIdx, rightIdx);
      tokens.push_back(token);
      
      return tokens;
    }
    
    std::string getSubstring(const std::string& str, int leftIdx, int rightIdx)
    {
      return str.substr(leftIdx, rightIdx - leftIdx + 1);
    }
    

提交回复
热议问题