2012-11-22 171 views
2

好的,所以我一直在寻找一种算法,可以帮助我在子字符串中找到一个字符串。 我之前使用的代码来自similar question,但它没有这样做。在字符串C++中查找子字符串(在“hello”中查找“el”)

// might not be exposed publicly, but could be 
int index_of(string const& haystack, int haystack_pos, string const& needle) { 
    // would normally use string const& for all the string parameters in this 
    // answer, but I've mostly stuck to the prototype you already have 

    // shorter local name, keep parameter name the same for interface clarity 
    int& h = haystack_pos; 

    // preconditions: 
    assert(0 <= h && h <= haystack.length()); 

    if (needle.empty()) return h; 
    if (h == haystack.length()) return -1; 
    if (haystack.compare(h, needle.length(), needle) == 0) { 
    return h; 
    } 
    return index_of(haystack, h+1, needle); 
} 

int index_of(string haystack, string needle) { 
    // sets up initial values or the "context" for the common case 
    return index_of(haystack, 0, needle); 
} 

这不会返回字符串“hello”上的“el”开始索引,我无法弄清楚。

编辑: OK,让我告诉你更多的包括一些现实生活中的例子代码: 我试图分析一个字符串,它是一个文件我想在我的文件系统来排序的路径。 输入例如是这样的:

输入:/media/seagate/lol/Sons.of.Anarchy.S04.720p.HDTV.x264/Sons.of.Anarchy.S04E01.720p.HDTV.x264-IMMERSE。 mkv

当我尝试通过检测SxxExx的存在来解析此字符串以获取其名称时,我查找“s0”,“S0”等(我知道这不是我刚才尝试的最佳实现看看它是否工作,然后看看代码)。所以,当我使用该输入,我所得到的输出是:

input:/media/seagate/lol/Sons.of.Anarchy.S04.720p.HDTV.x264/Sons.of.Anarchy.S04E01.720p.HDTV.x264-IMMERSE.mkv 

aux: 0p.HDTV.x264-IMMERSE.mkv 

input:/media/seagate/lol/Sons.of.Anarchy.S04.720p.HDTV.x264/Sons.of.Anarchy.S04E01.720p.HDTV.x264-IMMERSE.mkv 

aux: 1.720p.HDTV.x264-IMMERSE.mkv 

input:/media/seagate/lol/Sons.of.Anarchy.S04.720p.HDTV.x264/Sons.of.Anarchy.S04E01.720p.HDTV.x264-IMMERSE.mkv 

aux: 264-IMMERSE.mkv 

预期输出AUX:S04E01.720p.HDTV.x264-IMMERSE.mkv

因此,大家可以看到,它只是看对于字符串中的任何字符和停止,这也占多个有效的“找到”应该只是一个。

在那里我试图用这个是全码:

bool StringWorker::isSeries(size_t &i) { 

    size_t found1, found2, found3, found4, found5, found6; 
    found1 = input->find_last_of("S0"); //tried several find functions including the 
    found2 = input->find_last_of("S1"); //index_of() mentioned above in the post 
    found3 = input->find_last_of("S2"); 
    found4 = input->find_last_of("s0"); 
    found5 = input->find_last_of("s1"); 
    found6 = input->find_last_of("s2"); 

    if (found1 != string::npos) { 
     if (input->size() - found1 > 6) { 
      string aux = input->substr(found1, input->size()); 
      cout << "input:" << *input << endl; 
      cout << "aux: " << aux << endl; 
      if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2)) 
        && isalpha(aux.at(3)) && isdigit(aux.at(4)) 
        && isdigit(aux.at(5))) { 
       i = found1; 
       return true; 
      } 
     } 
    } 
    if (found2 != string::npos) { 
     if (input->size() - found2 > 6) { 
      string aux = input->substr(found2, input->size()); 
      cout << "input:" << *input << endl; 
      cout << "aux: " << aux << endl; 
      if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2)) 
        && isalpha(aux.at(3)) && isdigit(aux.at(4)) 
        && isdigit(aux.at(5))) { 
       i = found2; 
       return true; 
      } 
     } 
    } 

    if (found3 != string::npos) { 
     if (input->size() - found3 > 6) { 
      string aux = input->substr(found3, input->size()); 
      cout << "input:" << *input << endl; 
      cout << "aux: " << aux << endl; 
      if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2)) 
        && isalpha(aux.at(3)) && isdigit(aux.at(4)) 
        && isdigit(aux.at(5))) { 
       i = found3; 
       return true; 
      } 
     } 

    } 
    if (found4 != string::npos) { 
     if (input->size() - found4 > 6) { 
      string aux = input->substr(found4, input->size()); 
      cout << "input:" << *input << endl; 
      cout << "aux: " << aux << endl; 
      if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2)) 
        && isalpha(aux.at(3)) && isdigit(aux.at(4)) 
        && isdigit(aux.at(5))) { 
       i = found4; 
       return true; 
      } 
     } 

    } 
    if (found5 != string::npos) { 
     if (input->size() - found5 > 6) { 
      string aux = input->substr(found5, input->size()); 
      cout << "input:" << *input << endl; 
      cout << "aux: " << aux << endl; 
      if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2)) 
        && isalpha(aux.at(3)) && isdigit(aux.at(4)) 
        && isdigit(aux.at(5))) { 
       i = found5; 
       return true; 
      } 
     } 

    } 
    if (found6 != string::npos) { 
     if (input->size() - found6 > 6) { 
      string aux = input->substr(found6, input->size()); 
      cout << "input:" << *input << endl; 
      cout << "aux: " << aux << endl; 
      if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2)) 
        && isalpha(aux.at(3)) && isdigit(aux.at(4)) 
        && isdigit(aux.at(5))) { 
       i = found6; 
       return true; 
      } 
     } 

    } 

    return false; 

} 

你能看到这里有什么问题?

+7

['std :: string :: find'](http://en.cppreference.com/w/cpp/string/basic_string/find)有什么问题? –

+1

'std :: search'有什么问题? –

+0

适合我。您必须错误地使用该功能。发布完整的程序。 – john

回答

6

为什么不使用std::stringfind()方法 - >link

+0

因为std :: string的find()方法在传递一个字符串“el”时将返回“e”的第一个匹配项,即使没有后跟“l”也会导致错误。 – JackTakahashi

+0

@JackTakahashi我不这么认为 –

+1

看看我编辑原始帖子的例子。你会明白我的意思。 – JackTakahashi

3

此代码返回通过index = sub_str.find("el")指数:

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    string sub_str="abc def ghi jk lmnop hello"; 

    string sub_str2; 
    size_t index; 


    index = sub_str.find("el");  
    sub_str2 = sub_str.substr (index); 

    cout<<"index = "<<index<<"\n"; 
    cout<<sub_str2<<"\n"; 

    return 0; 
} 
0

用于查找字符串中的子串和它的索引,你可以尝试了这一点 -

int find_sub(const std::string& mstring,sub) 
{ 
    int lensub=sub.length(),len=mstring.length(),f=0,pos; 
    std::string b=""; 
    for(int i=0;i<len-lensub;i++) 
    { 
    for(int j=i,k=0;j<i+lensub;j++,k++) 
     b[k]=mstring[j]; 
    if(b.compare(sub)==0) 
    { 
     f=1; 
     pos=i; 
     break; 
    } 
    } 
    if(f==1) 
    cout<<"substring found at: "<<pos+1; 
    else 
    cout<<"substring not found!"; 
    return f; 
} 

您还检查字符串出现了多少次通过删除break;并每次增加f的值。也可以通过将pos转换为数组来获得索引。

+0

OP使用'std :: string',问题标记为'C++'。如果你真的想帮忙,试着去理解为什么OP代码是错误的。 – manuell

+0

也是三年前的问题 –

+0

@manuell我认为我的代码可以转换为可用于'std :: string',无论如何我编辑它。我使用数组是因为我不太熟悉std :: string。 “你是什么意思”标签为“C++'”?我其实写了一个C++代码。 – TubbyStubby