reformatting a string - help please

D

Dj Frenzy

Hi,
this should be a pretty easy question for the advanced people on
here. Basically , I have two strings:

string transDate = "20030901";
string transTime = "1200";

I want to know the best way to reformat them into a more recognisable
format so that:

transDate = "01/09/2003";
transTime = "12:00";

At the moment I just have a function like this:

string formatDate (string &oldFormat)
{
string newFormat;

newFormat.push_back(oldFormat[6]);
newFormat.push_back(oldFormat[7]);
newFormat.push_back('/');
newFormat.push_back(oldFormat[4]);
newFormat.push_back(oldFormat[5]);
newFormat.push_back('/');
newFormat.push_back(oldFormat[0]);
newFormat.push_back(oldFormat[1]);
newFormat.push_back(oldFormat[2]);
newFormat.push_back(oldFormat[3]);
return newFormat;
}

The function for formatTime is very similar. Can anyone give me any
help on how to use better programming to change the format, using
something from the standard template library maybe?

Cheers
Dave
 
M

Mike Wahler

Dj Frenzy said:
Hi,
this should be a pretty easy question for the advanced people on
here. Basically , I have two strings:

string transDate = "20030901";
string transTime = "1200";

I want to know the best way to reformat them into a more recognisable
format so that:

transDate = "01/09/2003";
transTime = "12:00";

At the moment I just have a function like this:

string formatDate (string &oldFormat)
{
string newFormat;

newFormat.push_back(oldFormat[6]);
newFormat.push_back(oldFormat[7]);
newFormat.push_back('/');
newFormat.push_back(oldFormat[4]);
newFormat.push_back(oldFormat[5]);
newFormat.push_back('/');
newFormat.push_back(oldFormat[0]);
newFormat.push_back(oldFormat[1]);
newFormat.push_back(oldFormat[2]);
newFormat.push_back(oldFormat[3]);
return newFormat;
}

The function for formatTime is very similar. Can anyone give me any
help on how to use better programming to change the format, using
something from the standard template library maybe?

std::string has what you need. Consider:

#include <iostream>
#include <string>

std::string fmt_date(const std::string& date,
char delim = '/')
{
return date.substr(6, 2) + delim +
date.substr(4, 2) + delim +
date.substr(0, 4);
}

int main()
{
std::string transDate = "20030901";
std::string transTime = "1200";

std::cout << transDate << '\n'
<< fmt_date(transDate) << '\n';

return 0;

}


There are of course many more ways to skin this
particular cat. I think you can take it from
here and do the time formatting.

HTH,
-Mike
 
G

Guest

Hi,
this should be a pretty easy question for the advanced people on
here. Basically , I have two strings:

string transDate = "20030901";
string transTime = "1200";

I want to know the best way to reformat them into a more recognisable
format so that:

transDate = "01/09/2003";
transTime = "12:00";

At the moment I just have a function like this:

string formatDate (string &oldFormat)
{
string newFormat;

newFormat.push_back(oldFormat[6]);
newFormat.push_back(oldFormat[7]);

std::string formatDate(string &oldFormat) {
std::string newFormat(oldFormat.size()+2,'/');
newFormat.replace(0,2,oldFormat,6,2);
newFormat.replace(3,2,oldFormat,4,2);
newFormat.replace(5,4,oldFormat,0,4);
return newFormat;
}
newFormat.push_back('/');
newFormat.push_back(oldFormat[4]);
newFormat.push_back(oldFormat[5]);
newFormat.push_back('/');
newFormat.push_back(oldFormat[0]);
newFormat.push_back(oldFormat[1]);
newFormat.push_back(oldFormat[2]);
newFormat.push_back(oldFormat[3]);
return newFormat;
}

The function for formatTime is very similar. Can anyone give me any
help on how to use better programming to change the format, using
something from the standard template library maybe?

Cheers
Dave
 
D

Dj Frenzy

cheers. i got it working no problem, much appreciated.

just a quick question though. in your answer you used

std::string instead of just string.

Whys this? Is it just a better way for programmers to know whats going
on in your code? Or does it make any difference to the program its
self?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top