15

i have a string and i want to get, for example, the position of the last (.) in the string, or whatever char i want to check, but untill now i just get a headeach.

thanks

2
  • 8
    Show us your code so far. What works, and what do you need help with?
    – abelenky
    Commented Dec 15, 2010 at 22:53
  • 1
    sorry i'm late to answer your comments, but i had some conection issues for all over a day, thanks to all for the answers
    – Castro Roy
    Commented Dec 16, 2010 at 16:27

4 Answers 4

19
string lastN(string input)
{
     return input.substr(input.size() - n);
}
3
  • 3
    Where does N comes from? Commented Jul 26, 2017 at 17:39
  • 9
    that's unacceptable. input.size() could be less than n.
    – Pavel P
    Commented Aug 7, 2017 at 5:12
  • @karlphillip from the question. n means how many characters from the end of the string you want
    – crxyz
    Commented Apr 5, 2021 at 20:36
16

Is find_last_of what you need?

size_type find_last_of( const basic_string& str, size_type pos = npos ) const;

Finds the last character equal to one of characters in the given character sequence. Search finishes at pos, i.e. only the substring [0, pos] is considered in the search. If npos is passed as pos whole string will be searched.

3
  • 1
    That's not an answer, it's a guess. Commented Dec 15, 2010 at 22:54
  • 11
    Seems like a pretty good guess to me. Can't do much more than guess with this question. Commented Dec 15, 2010 at 23:03
  • this answer should at least point out where that function comes from. Is it a method of string? Do you need to include a header for it?
    – Pynchia
    Commented Jun 4 at 16:05
8

If your string is a char array:

#include <cstdio>
#include <cstring>

int main(int argc, char** argv)
{
 char buf[32] = "my.little.example.string";
 char* lastDot = strrchr(buf, '.');
 printf("Position of last dot in string: %i", lastDot - buf);
 return 0;
}

..or a std::string:

#include <cstdio>
#include <string>

int main(int argc, char** argv)
{
 std::string str = "my.little.example.string";
 printf("Position of last dot in string: %i", str.find_last_of('.'));
 return 0;
}
1
  • 1
    <stdio.h> is nonstandard. Use <cstdio>. Same for <string.h>. Commented Dec 15, 2010 at 23:59
4
   #include <string>
    /**
    * return the last n characters of a string,
    * unless n >= length of the input or n <= 0, in which case return ""
    */
    string lastN(string input, int n)
    {
        int inputSize = input.size();
        return (n > 0 && inputSize > n) ? input.substr(inputSize - n) : "";
    }
3
  • 2
    Why not return input if its length is less or equal to n?
    – DrP3pp3r
    Commented Apr 23, 2020 at 16:27
  • Tested, it's working with C++98: ideone.com/IKJeI9
    – Lod
    Commented Oct 12 at 17:13
  • With string array working too with C++98: ideone.com/cNpBYA
    – Lod
    Commented Oct 12 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.