Solved

Help with formatting a couple of numbers in C#

Posted on 2013-06-24
5
162 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 39

Accepted Solution

by:
Kyle Abrahams earned 100 total points
Comment Utility
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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
Upped the points
0
 

Author Closing Comment

by:JDL129
Comment Utility
Thanks guys!!!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now