parsing a string into int/long using custom format strings
Asked Answered
B

3

6

In C#.Net, here's a simple example of how to format numbers into strings using custom format strings: (example taken from: http://www.csharp-examples.net/string-format-int/)

String.Format("{0:+### ### ### ###}", 447900123456); // "+447 900 123 456"
String.Format("{0:##-####-####}", 8958712551);       // "89-5871-2551"

Is there a way to convert this formatted string back into a long/integer ? Is there someway to do this :

long PhoneNumber = Int32.Parse("89-5871-2551", "{0:##-####-####}");

I saw that DateTime has a method ParseExact which can do this work well. But I did not see any such thing for int/long/decimal/double.

Byelaw answered 27/2, 2012 at 19:32 Comment(5)
Try this. Int32.Parse(("89-5871-2551").Replace("-", "")); ---- My answer below shows how to remove more than just the hyphens - before parsing the string.Correspond
Why are you storing phone numbers as ints/longs? A phone number is a sequence of digits, not a numeric value. Your life will be simpler if you store it as a string rather than as an integral data type.Proceleusmatic
@Proceleusmatic : I just used the phone numbers as an example. I have to deal with bank account numbers which are formatted a certain way for presentation, and that's how it comes in the dumps from other systems. I don't want to use strings cause the account numbers will participate in a lot of comparisons and we all know how bad strings are at that. System will be processing more than 20 million records, possibly 5 million at a time; so performance is a concern.Byelaw
The dumps are like CSVs. The parsing logic is part of a CSV library (driven by code-gen and annotations) and the solution had to be generic enough to work with all major data types. Hence, I finally chose to use Regex. Unfortunately, I have to use separate format strings for serializing (ToString overloads using a format string) and deserializing (regex to strip unwanted characters followed by parse), which I was trying to avoid.Byelaw
I would also like to see an answer to this but I am presuming there really isn't one. All the answers seem to be 'just do this instead' which isn't meeting my use case.Lennielenno
A
0

Just Regex out all of the non-numeric characters, then parse that string.

Adamina answered 27/2, 2012 at 19:36 Comment(0)
C
5

You can regex out all of the non numeric numbers, and what you're left with is a string of numbers that you can parse.

var myPhoneNumber = "89-5871-2551";
var strippedPhoneNumber = Regex.Replace(myPhoneNumber, @"[^\d]", "");
int intRepresentation;

if (Int32.TryParse(strippedPhoneNumber, out intRepresentation))
{
    // It was assigned, intRepresentation = 8958712551
    // now you can use intRepresentation.

} else {
    // It was not assigned, intRepresentation is still null.
}
Correspond answered 27/2, 2012 at 19:39 Comment(0)
C
1

Well, you can always do

long PhoneNumber = Int32.Parse("89-5871-2551".
                Replace(new char[]{'-','+',whatever..}).Trim());

By the way, considering that you're parsing a string received from some IO, I would suggest to use more secure (in terms of conversion) Int32.TryParse method.

The way like you described doesn't actually exist.

Compellation answered 27/2, 2012 at 19:36 Comment(3)
This is a bit of a pain to have to second guess each possible character that a user enters in their phone number. {'-', '_', '+', '*', '/', '\', '|', '<', '>', '.', ','} etc...Correspond
@ChaseFlorell: I presume program provided gets some expected input, if the variations of the possible symbols count that could appear in that string are too much, yes it's beter to use regular expressions. But unless there is <10 (say) I would chose mine code, cause it much more clear and understandable then regex symbolism.Compellation
you may be right, but I know that users get frustrated if you validate the hell out of them so that they are required to input exactly what you expect from them. You're better off being gracious on the front end (nicer user experience) at a cost of a few extra cpu cycles drilling down to the required data.Correspond
A
0

Just Regex out all of the non-numeric characters, then parse that string.

Adamina answered 27/2, 2012 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.