Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ string to vector using delimiter

/*----------------------------------------------------------
Converts a string into a vector of strings based on the delimiter given
------------------------------------------------------------*/
vector<string> ConvertStringToVectorUsingDelimiter(string MainString, string Delimiter)
{
   vector<string> ReturnVector;
   int iDelLen = Delimiter.length();

   string sCur = "";

   int iN = 1; // iN = occurrence of the delimiter within the string
   int iPrevInd = 0;
   int iCurrInd = GetIndexOfNthOccurrenceInString(MainString,Delimiter,iN);

   if (iCurrInd == -1)
   {
      ReturnVector.push_back(MainString);
      return ReturnVector;
   }

   while (iCurrInd != -1)
   {
      string sCur = MainString.substr( (iPrevInd + iDelLen) , iCurrInd - (iPrevInd + iDelLen) );
      ReturnVector.push_back(sCur);

      iPrevInd = iCurrInd;
      iN++;
      iCurrInd = GetIndexOfNthOccurrenceInString(MainString,Delimiter,iN);
   }

   return ReturnVector;
}

/*----------------------------------------------------------
Gets the nth occurrence of the substring within the string
------------------------------------------------------------*/
int GetIndexOfNthOccurrenceInString(string sMain, string sSubstr, int iN)
{
   int iIndex = -1;
   size_t stIndex = 0;

   switch (iN)
   {
      case -1: // Uuser wants the very last occurrence of sSubstr
         stIndex = sMain.find_last_of(sSubstr);
         if (stIndex == string::npos)
         {
            return -1;
         }

         return int(stIndex);

      case 0: // provide for if the user asks for the 0th occurrence (make this the first occurence)
         iN = 1;
         break;
   }

   int iCurOccurrence = 0;

   while ( (iCurOccurrence != iN) && (iCurOccurrence < sMain.size()) )
   {
      stIndex++;
      stIndex = sMain.find(sSubstr, stIndex);
      if (stIndex == string::npos)
      {
         return -1;
      }
      iCurOccurrence++;
      iIndex = int(stIndex);
   }

   int iPos = int(stIndex);
   return iIndex;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: cpp cout more than 1 value 
Cpp :: swift functions from cpp 
Cpp :: how to print std::string 
Cpp :: char to binary 
Cpp :: how to user input in string to open files in c++ 
Cpp :: std::hash 
Cpp :: c++ set value to inf 
Cpp :: c++ 
Cpp :: an array that take different data type c++ 
Cpp :: C++ with SVD 
Cpp :: run a c++ file in terminal 
Cpp :: convert char to C 
Cpp :: c++ program to use nmap 
Cpp :: asio broadcast permission 
Cpp :: comment savoir si un nombre est premier c++ 
Cpp :: c++ language 
Cpp :: How to assign two dimensional initializer list in c++ 
Cpp :: get player pawn 
Cpp :: vprintf 
Cpp :: c++ string not printing 
Cpp :: onactorbeginoverlap c++ 
Cpp :: qrandomgenerator bounded 
Cpp :: how to declare a function in c++ header file 
Cpp :: c++ how to get maximum value 
Cpp :: deletion in bst 
Cpp :: use of strtok 
Cpp :: c++ function with parameters 
C :: auto click connect colab 
C :: boolean in c 
C :: shuffle function in c 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =