Solved

Help with formatting a couple of numbers in C#

Posted on 2013-06-24
5
168 Views
Last Modified: 2013-06-24
Hey guys!!

I have a date string
                    InitVar.strDOB = AllRx.Rows[odernum]["DOB"].ToString();
 I need to format as follows:
                    19460803

And also a phone number
                    InitVar.strCustPhone = AllRx.Rows[odernum]["PHONE"].ToString();
I need to format as follows:
                    4795555555

Thanks a bunch,
Jerry
0
Comment
Question by:JDL129
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 100 total points
ID: 39272621
For the first:
DateTime.Parse(InitVar.strDOB).ToString("yyyymmdd");
For the second:


for the second:
  string s = "(515) 555 - 2323";
                string t = "";
                foreach( char c in s.ToCharArray())
                    if (char.IsDigit(c))
                        t += c;
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 39272645
Hi JDL129;

The following should do what you need. In the PHONE if there are other formatting characters other then ( ) - add them to the part of the Regex pattern in "[/() -]", after the "[ and before -, the - should be the last one.


using System.Text.RegularExpressions;

InitVar.strDOB = DateTime.Parse(AllRx.Rows[odernum]["DOB"].ToString()).ToString("yyyyMMdd");

InitVar.strCustPhone = Regex.Replace(AllRx.Rows[odernum]["PHONE"].ToString(),
                             "[/() -]", "");

Open in new window

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39272700
Just noticed that the format string in mine had small m's.  Those are for minutes . . . capital M's is correct for months.

eg:
 ToString("yyyyMMdd");
0
 

Author Comment

by:JDL129
ID: 39272823
Upped the points
0
 

Author Closing Comment

by:JDL129
ID: 39272827
Thanks guys!!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question