Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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") << "
"; // output: and the 

    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: Opengl GLFW basic window 
Cpp :: c++ argument list for class template is missing 
Cpp :: reverse a stack in c++ using another stack 
Cpp :: sort vector in descending order c++ 
Cpp :: TCA9548 I2CScanner Arduino 
Cpp :: c++ terinary operator 
Cpp :: accepting string with space on same line C++ 
Cpp :: dinamic 
Cpp :: library management system project in c++ using array 
Cpp :: c++ map values range 
Cpp :: number of characters in string 
Cpp :: passing array to the function c++ 
Cpp :: Your age doubled is: xx where x is the users age doubled. (print answer with no decimal places) 
Cpp :: heroatx77 
Cpp :: beecrowd problem 1003 solution in c++ 
Cpp :: c++ define function in header 
Cpp :: how to move your chrector in unity 
Cpp :: determining a string is subsequence of another 
Cpp :: can map return a value to a variable in c++ 
Cpp :: Targon lol 
Cpp :: days in a year c++ 
Cpp :: C++ operation 
Cpp :: glm multiply vector by scalar 
Cpp :: lnk2001 unresolved external symbol __imp_PlaySoundA 
Cpp :: C++ (ISO) 
Cpp :: for in c++ example 
Cpp :: sin trigonometric function 
Cpp :: pca compact trick 
Cpp :: how to make a goto area in c++ 
Cpp :: c++ coding questions for interview 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =