Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with formatting a couple of numbers in C#

Posted on 2013-06-24
5
Medium Priority
?
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 400 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

664 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