Answers for "c++ string get substring between two characters"

C++
0

c++ get string between two characters

string str = "STARTDELIMITER_0_192.168.1.18_STOPDELIMITER";
unsigned first = str.find(STARTDELIMITER);
unsigned last = str.find_last_of(STOPDELIMITER);
string strNew = str.substr (first,last-first);
std::cout << strNew << std::endl;
Posted by: Guest on May-09-2021
0

c++ text between substrings

////////////////////////////////////////////////////////////////////////
// Use the GetTextBetweenSubstrings function (see main for example)
////////////////////////////////////////////////////////////////////////


#include <iostream>
using namespace std;

int GetIndexOfNthOccurrenceInString(string sMain,string sSubstr, int iN);
string GetTextBetweenSubstrings(string WholeStr, string TextA, string TextB, bool LookFromEnd);

/*==================================================================================================================================
 * GetTextBetweenSubstrings()
 *================================================================================================================================*/
/*!
 * Returns the text that can be found between TextA and TextB (TextA must be left of TextB)
 */
/*================================================================================================================================*/
string GetTextBetweenSubstrings(string WholeStr, string TextA, string TextB, bool LookFromEnd = false)
{
   int iTextAInd = -1;
   int iTextBInd = -1;

   // Find the first/last occurence of TextB
   if (LookFromEnd)
   {
      iTextBInd = GetIndexOfNthOccurrenceInString(WholeStr, TextB, -1);
   }
   else
   {
      iTextBInd = GetIndexOfNthOccurrenceInString(WholeStr, TextB, 1);
   }

   // Find the last occurence of TextA before TextB
   iTextAInd = GetIndexOfNthOccurrenceInString(WholeStr.substr(0,iTextBInd), TextA, -1);

   if ( (iTextAInd == -1) || (iTextBInd == -1) )
   {
      return "FAILED_STRING"; // Either TextA or TextB does not exist in the WholeStr string
   }

   // Return the substring between the texts
   string sFinal = WholeStr.substr( (iTextAInd + 1) , iTextBInd - (iTextAInd + 1) );

   return sFinal;
}

/*==================================================================================================================================
 * GetIndexOfNthOccurrenceInString():
 *================================================================================================================================*/
/*!
 * 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: // User 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;
}

int main()
{
    string sMainTxt = "Find text between the A and the B";
    cout << "Text found: " << GetTextBetweenSubstrings(sMainTxt,"A","B") << "\n"; // output: and the 

    return 0;
}
Posted by: Guest on February-08-2022

Code answers related to "c++ string get substring between two characters"

Browse Popular Code Answers by Language