2013-07-17 36 views
2

我有它的HTML大的文本文件,我想以後每隔「</b>」添加一個空格(粗體的每一個字後)檢測文本的特定字符串的文件

文本lenght約581 810

,我不知道如何正確地做到這一點,我想試試這個:

1,創建一個名爲「v」串矢量

2,獲取文本的每一個字符(不知道如何做到這一點,我可以得到線條和文字,但我不知道如何獲得特徵cters)在此向量(與推背和另一個字符串)

3-檢測每一個「</b>」與「爲」像這樣的循環:

for(int i = 0; i < 581810; i++) 
{ 
    if (v[i] + v[i+1] + v[i+2] + v[i+3] == "</b>"){ 

     // add a space after </b> (don't know how to this) 

    } 
} 

,但我不知道讓每一個單在我的字符串向量中的字符,我知道如何獲得線條,getline和帶有「>>」的單詞。我無法用語言做,因爲HTML標籤貼的話

感謝

+0

您是否必須使用C++? – nouney

+0

是否有任何特定的原因,你想在C++中做到這一點,或者你只是在尋找一個解決方案? – jpw

+0

你需要一個漂亮的解決方案,並不假設字符只有581810. – turnt

回答

0

http://ideone.com/KZsyc6

沒有做任何幻想(正則表達式,shell命令),你可以做這樣的事情:

const std::string bTag("</b>"); 
std::string line; 
size_t indexOfBTag=0; 
for(...) //iterate through your file, line by line 
{ 
    //populate line with current line from file via getline, up to you 

    //store position of the b tag into indexOfBTag and make sure that theres a b tag in your line 
    //make sure to search starting after the last BTag found, or this loop will never end 
    //however, if the index is 0 (meaning the first search), dont bother adding the size 
    //hence the find(..., indexOfBTag > 0 ? indexOfBTag + bTag.size() : 0) 
    while((indexOfBTag = line.find(bTag, indexOfBTag > 0 ? indexOfBTag + bTag.size() : 0)) != std::string::npos) { 
    line.insert(line.begin() + indexOfBTag + bTag.size(), ' '); 
    } 
} 
相關問題