从字符串中打印HTML标记

时间:2015-03-13 17:04:52

标签: c++ c++11

我正在尝试打印字符串中包含的所有html标记。这里的代码逻辑似乎有些问题,我不断得到一个永无止境的循环。

string fileLine;
string strToPush;

fileLine ="<html> sdad </b>as"; // My string that I want to find tags from

  cout << fileLine << '\n';

  while(!fileLine.length()==0)
  {

      if(fileLine.at(0)=='<')
        { 
            strToPush = "";
            while(!fileLine.at(0)=='>') 
            {
            strToPush = strToPush + fileLine.at(0);
            fileLine.erase (0); 
            }

            cout << endl << "HTML Tag detected: " << strToPush <<endl;

        }
        else
        {
        fileLine.erase (0);
        }
  }

1 个答案:

答案 0 :(得分:2)

我怀疑你被运营商优先权绊倒了。这条线

while(!fileLine.length()==0)

相当于

while( (!fileLine.length()) == 0 )

你可能想做的是:

while( !(fileLine.length() == 0) )

您可以将其简化为:

while( !fileLine.empty() )

同样,更改行

while(!fileLine.at(0)=='>') 

while ( fileLine.at(0) != '>' ) 
相关问题