CPP/Strings/SingleCharacterTag

From ProgrammingExamples
< CPP
Jump to: navigation, search

SingleCharacterTag.cpp

#include <iostream>
#include <string>
 
int main()
{
  size_t firstPos = 0, secondPos;
  std::string temp = "some text a <tag> and more text";
  std::string sub;
  if ((firstPos = temp.find_first_of("<", firstPos)) != std::string::npos)
  {
    if ((secondPos = temp.find_first_of(">", firstPos + 1)) != std::string::npos)
    {
      sub = temp.substr(firstPos, (secondPos - firstPos + 1));
    }
  }
 
  std::cout << sub << std::endl;
 
  return 0;
}