473,835 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reformatting a string - help please

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
Jul 19 '05 #1
3 1824

"Dj Frenzy" <da******@hotma il.com> wrote in message
news:81******** *************** ***@posting.goo gle.com...
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(transD ate) << '\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
Jul 19 '05 #2
On 15 Oct 2003 09:14:10 -0700, Dj Frenzy <da******@hotma il.com> wrote:
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(stri ng &oldFormat) {
std::string newFormat(oldFo rmat.size()+2,'/');
newFormat.repla ce(0,2,oldForma t,6,2);
newFormat.repla ce(3,2,oldForma t,4,2);
newFormat.repla ce(5,4,oldForma t,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

--
grzegorz
Jul 19 '05 #3
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?
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
6571
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help Workshop program: hcw.exe that's included with Visual Basic. This exact same file compiled perfectly with no notes, warnings or errors prior to reformatting my system. Prior to the reformatting, I copied the help.rtf file onto a CD and checked the box to...
11
2070
by: bearophile | last post by:
Hello, here are a four more questions (or suggestions) for the language (probably people have already discussed some of/all such things: I've seen the contracts for Python: http://www.wayforward.net/pycontract/ http://www.python.org/peps/pep-0316.html They look interesting and nice, how Python developers feel about accepting something like this in the standard language? (Maybe they are a bit complex).
0
1099
by: Draz | last post by:
I have some XML extended language that generates output. Everything works ok. I have created additional xsl templates for reformatting strings, mathematical calculations and reformatting numbers. It also works ok. Finally, I'm missing one step. I need to put all these results as values closed in quotes, everything in one line. Example input is:
7
1622
by: David Jacques | last post by:
I have to reformat a string from the form "SRID=4269;POINT(-90.673 69.4310000006199)" to GeometryFromText('POINT (-141.095 68.5430000006417)',4269) ); I have a function to do this by taking the input string as char* and returning
3
1375
by: Chris Lane | last post by:
Hi, This is super annoying FrontPage like behavior. Does anybody know how I can stop the Visual Studio.NET IDE from reformatting my HTML? Thank You
7
1244
by: Lloyd Sheen | last post by:
I know I have seen a way to stop the terrible reformatting that VS 2003 IDE does. Beyond that is MS going to do a better (could not do a worse) job of auto formatting. Every change I make to the format of the page causes reformatting which totally gets rid of the idea that controls are nested and have some structure. Please help
6
2185
by: Abhishek Srivastava | last post by:
Hello All, I am developing a page which has a complex layout. This layout cannot be made using VS alone, so I have to go into the html view and edit the html myself. The problem is that everytime I save the file, (or add/remove any component to the page) VS.Net reformats and re-edits the whole page. I find this very irrating because VS.Net totally disorganizes my code.
13
1370
by: sck10 | last post by:
Hello, I am trying to find the option VS2K5 that will automatically change HtmlMeta hm=new HtmlMeta(); or HtmlMeta hm = new HtmlMeta(); to HtmlMeta hm = new HtmlMeta(); What I would like to do is have extra spaces taken out or added in where appropriate. I can do this in vb, but am having problems with c#.
2
1727
by: sparks | last post by:
last month I started on changing the format in a LOT of tables. changing Long Integer and Singles to double. I got that working but I have one more question.. Sub AlterFieldType(TblName As String, FieldName As String, DataType As _
0
9808
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10812
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10523
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10561
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9346
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5638
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3089
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.