////////////////////////////////////////////////////////////////////////
// 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;
}