C++ substr
I'm having trouble finding the last 3 characters of a string.
I have the following:
1 2 3 4 5 6 7 8 9
|
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
Just substr(result).
Never mind, I figured it out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#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; // returns 'e'
cout << "Last 3 characters: " << text.substr( text.size() - 3 ) << endl; // returns 'ide'
system("pause");
return 0;
}
|
Topic archived. No new replies allowed.