Jump to content

C++ substr


MasterACE14

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.