Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substring problem

Status
Not open for further replies.

prrm333

Programmer
Apr 14, 2003
97
0
0
US
I am returning a phone number from a database stored as all 10 digits.

I want to parse it out to look like:
(###) ###-####

I am having trouble understand how to accomplish this from the following returning from the database:

string strModified = dt["HPhone"].ToString();
 
Code:
//not sure if this works or not.
var number = 1234567890;
var as_string = number.ToString("000-000-0000");
or
Code:
var number = 1234567890.ToString();
var as_string = string.Format("{0}-{1}-{2}", number.Left(3), number.SubString(4,3), number.Right(4));


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
The second method doesn't like the left and right function.
The first method throws errors about invalid arguements.
Not sure what to try next.
 
var as_string = string.Format("{0}-{1}-{2}", number.SubString(0, 3), number.SubString(4,3), number.SubString(7));

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
The second method doesn't like the left and right function.
how? exception messages and stack trace is full of useful information.


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Using the following:

var number = dt["LastName"].ToString();
var as_string = string.Format("{0}-{1}-{2}", number.SubString(0, 3), number.SubString(4, 3), number.SubString(7));

I am getting the following error:

Error 1 'string' does not contain a definition for 'SubString' and no extension method 'SubString' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
 
The code I used should be as follows:

var number = dt["HPhone"].ToString();
var as_string = string.Format("{0}-{1}-{2}", number.SubString(0, 3), number.SubString(4, 3), number.SubString(7));
 
Thanks for the help as I solved the problem. I was using the following part incorrect:

string hPhone1 = hPhone1.Substring(0, 3);

I had started out with:

string hPhone1 = hPhone1.Substring(1, 3);

So the index was incorrect so I changed it to reflect the correct location within the string and I ended up with the following which works correctly:

string hPhone1 = string.Format(dt["HPhone"].ToString());
if (hPhone1 != "")
{
string hPhone1 = hPhone1.Substring(0, 3);
string hPhone2 = hPhone1.Substring(3, 3);
string hPhone3 = hPhone1.Substring(6, 4);
string hPhone2 = "(" + hPhone1 + ")" + " " + hPhone2 + "-" + hPhone3;
}

So I finally realized that the index is 0-9 and not 1-10.
 
Formatting phone numbers is a fairly common requirement. Rather than having this inline in your code, it would be a good idea to refactor it out to a separate method. Then, when you discover later that not all of your phone numbers conform to a nice 10-digit format, you only have one place to go to fix it...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
At one time I started on writing a library to format phone numbers based on the CultureInfo object. It turned out to be much harder than I ever expected, and I gave up.

Be grateful that the North American Numbering Plan is such a good design.

Chip H.



____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top