在较长字符串中查找字符串 [英] Find string within longer string

查看:147
本文介绍了在较长字符串中查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我目前有一个文件名,我希望在其中搜索某个字符串,如果有的话,那么我希望根据具体的任务做什么我发现子串。



示例:



testDir / ham / test1.1

testDir / ham / test2.2

testDir / spam / test3.3

testDir / spam / test4.4



假设文件名存储为strFilename中的字符串。如果我想分开HAM和SPAM文件夹,我能使用strFilename.find(/ ham /)吗?到目前为止,在我的代码中,使用它只返回除了我想要的东西。



代码:

Hello,

I currently have a filename that I wish to search for a certain string within, if so, then I wish to do specific tasks based on what substring I found.

Example:

testDir/ham/test1.1
testDir/ham/test2.2
testDir/spam/test3.3
testDir/spam/test4.4

Let's say the filename is stored as a string in "strFilename". Would I be able to use strFilename.find("/ham/") if I wanted to seperate out the HAM and SPAM folders? So far, in my code, using this only returns everything except what I want.

Code:

<pre>int main(int argc, char* argv[] ) {

    int wordCount;
    int fileStrCount;
    int hamCount;
    int spamCount;
    
    set<string> seenWords;
    map<string, int> ham;
    map<string, int> spam;    



    if( argc < 2 ) {
        cout <<  "ERROR: Must have atleast one filename! \n";
    }
    //The user gives a path to a directory containing "index"
    //File is used to store filename and fList stores them
    string files = "";
    vector<string> fList;

    //Storing full path of directory to later add /index
    string pathToDir = argv[1];
    string pathToIndex = pathToDir + "index";
    ifstream dirIndex( pathToIndex );

    //Push back all filenames within index to fList
    while( getline( dirIndex, files ) ) {
        fList.push_back( files );
    }

    string thirdWord = "";




    //Iterate through filenames and open them 
    for(unsigned x = 0; x < fList.size(); x++) {
        string goHere = pathToDir + fList[x];
        ifstream fileToOpen( goHere );
        if( !fileToOpen.is_open() ) {
            cout << "Error: " << goHere << " cannot open!\n";
            return -1;
        }

        //Ignore first two lines
        fileToOpen.ignore( numeric_limits<streamsize>::max(), '\n' ); 
        fileToOpen.ignore( numeric_limits<streamsize>::max(), '\n' ); 
        cout << endl << goHere << endl;

        if( goHere.find("/ham/") == true ) { 
        while( getline( fileToOpen, thirdWord, ' ' ) ) {
            ++ham[thirdWord];
            ++hamCount;
            wordCount++;
        }
    } else {
            while(getline(fileToOpen, thirdWord, ' ') ) {
            ++spamCount;
            ++spam[thirdWord];
            wordCount++;
            }
           }

        cout << "Total Words In File: " << fileStrCount << endl;

        fileToOpen.close();
        fileToOpen.clear();




    }
    cout << endl << endl << endl << "HAM MAP\n";
    for(auto elem : ham)
    {
        cout << elem.first << " " << elem.second << "\n";
    }

     cout << endl << endl << endl << "SPAM MAP\n";
    for(auto elem : spam)
    {
        cout << elem.first << " " << elem.second << "\n";
    }

   

}





主要关注点:

if( goHere.find("/ham/") == true ) { 
        while( getline( fileToOpen, thirdWord, ' ' ) ) {
            ++ham[thirdWord];
            ++hamCount;
            wordCount++;
        }
    } else {
            while(getline(fileToOpen, thirdWord, ' ') ) {
            ++spamCount;
            ++spam[thirdWord];
            wordCount++;
            }
           }





我的尝试:



我试过调查str :: substr()但这不是我想要的。



What I have tried:

I've tried looking into str::substr() but that isn't what I want either.

推荐答案

您选择了适当的 string :: find - C ++ Reference [ ^ ]函数,但错过了它没有返回布尔值:

You choosed the appropriate string::find - C++ Reference[^] function but missed that it does not return a boolean value:
Quote:

返回值

第一个字符的位置第一场比赛。

如果没有找到匹配项,该函数返回string :: npos。

Return Value
The position of the first character of the first match.
If no matches were found, the function returns string::npos.

所以你必须将你的代码更改为:

So you have to change your code to:

if( goHere.find("/ham/") != string::npos )


这篇关于在较长字符串中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆