MasterACE14 Posted January 12, 2012 Share Posted January 12, 2012 I'm having trouble finding the last 3 characters of a string. I have the following: string something; char result; string last3; something = "12345"; result = something.length()-3; last3 = something.substr(result, 0); cout << last3 << endl; I want it to output '345', but I'm getting '2'. Any help is appreciated, thanks. Regards, Ace Link to comment https://forums.phpfreaks.com/topic/254923-c-substr/ Share on other sites More sharing options...
MasterACE14 Posted January 12, 2012 Author Share Posted January 12, 2012 sorry, posted in wrong sub-forum. Link to comment https://forums.phpfreaks.com/topic/254923-c-substr/#findComment-1307089 Share on other sites More sharing options...
MasterACE14 Posted January 13, 2012 Author Share Posted January 13, 2012 never mind, I figured it out. #include <iostream> #include <string> using namespace std; int main() { string text = "I do like the seaside"; cout << "Original: " << text << endl; cout << "Last character: " << text.at( text.size() - 1 ) << endl; cout << "Last 3 characters: " << text.substr( text.size() - 3 ) << endl; system("pause"); return 0; } Link to comment https://forums.phpfreaks.com/topic/254923-c-substr/#findComment-1307103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.