[help]how to copy these last three character?

anyone know how to copy the last three characters of LName from this data structure?

1
2
3
4
5
6
7
8
9
10
struct shipInfo
{
       string shipDate;
       int PartNo;
       string TrackingNo;
       string FName;
       string LName;
       string Company;
};


1
2
3
4
5
shipInfo ship[numrecs] = {{"12/01/2014",50625,"C74444","Robo","Cop","SeePlusPlus"},
                               {"31/01/2014",60752,"X75255","Power","Rangers","Whatsup"},
                               {"10/02/2014",40295,"G74477","Kiki","Lalat","Bingo&Co"},
                               {"15/03/2014",23745,"A71232","Cicak","Man","LizardoLtd"},
                               {"12/01/2014",70892,"M76321","Miki","Maus","CarToon"}};
First use size() to find the length of the string, then use substr() to obtain the required part of the string.

1
2
3
4
    string temp;
    size_t len = ship[i].LName.size();
    if (len >= 3)
        temp = ship[i].LName.substr(len-3);


http://www.cplusplus.com/reference/string/string/size/
http://www.cplusplus.com/reference/string/string/substr/
Topic archived. No new replies allowed.