Thread: Find index of last char in search string in string?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Find index of last char in search string in string?

    Hello.
    I want to see if one string contains another string, and if so, return the index of the last char of the search string in the source string.

    string::find() wont work for me because it returns the index of the first char of the search string, not the index of the last.

    Do I need to write my own function?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Consider: using string::find(), you obtain the index of the first char found. Using string::length(), you obtain the length of the string being searched for. Add the them, subtract 1, and you get the index of the last char of the search string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Consider: using string::find(), you obtain the index of the first char found. Using string::length(), you obtain the length of the string being searched for. Add the them, subtract 1, and you get the index of the last char of the search string.
    Ok, thanks.
    I wrote a quick function implementing this approach.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Actually you wanted string::rfind() when you consider:
    Code:
    string strSearch = "foo";
    string strData = "stew-foo-foobar";
    int npos = strData.rfind(strSearch);
    if( npos != std::string::npos)
    {
        npos += strSearch.size();
    }
    or something like this. I just got home, its 111 degrees in the shade and I have since put my brain on a shelf.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jeffcobb View Post
    Actually you wanted string::rfind() when you consider:
    Code:
    string strSearch = "foo";
    string strData = "stew-foo-foobar";
    int npos = strData.rfind(strSearch);
    if( npos != std::string::npos)
    {
        npos += strSearch.size();
    }
    or something like this. I just got home, its 111 degrees in the shade and I have since put my brain on a shelf.
    No, because I wasn't trying to find the last occurance of a string in a string. I wanted to find the first occurance of a string in a string, then get the index of the last character of that string in a string. Savvy?
    Besides, I already got it to work with laserlight's method.

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    OK; you weren't specific and all I got was "string::find() won't work because it returns the first occurrance...". No biggie as long as you only have one instance of the search string and that's all you want...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jeffcobb View Post
    OK; you weren't specific and all I got was "string::find() won't work because it returns the first occurrance...". No biggie as long as you only have one instance of the search string and that's all you want...
    Yeah, that's all I want, because I read a single-line enum, enum value by enum value (comma to comma). I then use the function mentioned to find the position of the last char in the enum value name of the line read from that contains 'enum'. And it works well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help :(
    By ramenen in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2010, 04:31 PM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  5. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM