title: MFC Extract CString between tags
body: <p>I'm trying to code a function to retrieve the value between two tags or characters using CStrings, so far I haven't been able to do so.</p>
  
  <pre><code>CODE REMOVED
  </code></pre>
  
  <p>I'm pretty sure StartIndex and EndIndex have the right values, however I'm stuck at the last step, where I'm supposed to extract the substring from between the tags.</p>
  
  <p>EDIT:// Got it working, thanks to Igor Tandetnik. If anyone knows why SubStr only prints correctly with wcout if I explicitly cast it with <code>(LPCWSTR)</code> it would be greatly appreciated. I'm leaving the working code below in case someone else needs this or wants to improve it.</p>
  
  <pre><code>CString ExtractString(CString strSource, CString strStart, CString strEnd)
  {
      CString SubStr = L"";
      int iEndIndex, iStartIndex = strSource.Find(strStart);
      iStartIndex += strStart.GetLength();
      iEndIndex = strSource.Find(strEnd, iStartIndex);
  
      if (iStartIndex == -1 || iEndIndex == -1)
      {
          wcout &lt;&lt; L"TAG not found!" &lt;&lt; endl;
          return SubStr;
      }
  
      SubStr = strSource.Mid(iStartIndex, (iEndIndex - iStartIndex));
      SubStr.Trim();
      return SubStr;
  }
  </code></pre>