Solved

Help with formatting a couple of numbers in C#

Posted on 2013-06-24
5
169 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 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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