CString find the last entry

后端 未结 5 1931
无人共我
无人共我 2021-01-23 08:18

I have two CString s1 and CString s2. I need find the last entry s2 in s1. I can find any metod in CString like in C# LastIndexOf. I am nooby in c++.

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-23 08:39

    CString has no such function. You have to write it yourself, e.g.

    int LastIndexOf(const CString& s1, const CString& s2)
      {
      int found = -1;
      int next_pos = 0;
      for (;;)
        {
        next_pos = s1.Find(s2, next_pos);
        if (next_pos == -1)
          return found;
    
        found = next_pos;
        };
      }
    

    A more optimal algorithm would reverse the strings first, I'm leaving that as an exercise.

提交回复
热议问题