ToString(string) and parsing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Everyone,

If I want to format a type with a string format I can call
IFormattable.ToString(strFormat, formatProvider);

Does anyone know of a generic way to parse a string like this?
DateTime.ParseExact has an overload which takes these arguments but I can't
find a way to parse, for example, a number with a format string.

Any ideas? Thanks in advance, Jerry
 
Not quite sure if this is what you want:

Int32.Parse(String, IFormatProvider)
Double.Parse(String, IFormatProvider)

and so on.

If it does not meet your needs, you may use Regular Expressions.

Michael
 
Thanks Michael but I was looking for a Parse method which understands a
string format as in IFormattable.ToString(strFormat, formatProvider);
 
It sounds like you want to do something like formatting a phone number
string ("8885551212") in a specific format "(888) 555-1212". I don't know of
a generic way of doing this, but you can convert the value to a type that
has .ToString for formatting as in the following:

FormattedPhone = Integer.Parse(UnformattedPhone).ToString("(###)
###-####")

Of course, you would need to check that your input string can be converted
as necessary.

Jim
 
No, Jim, I am trying to _parse_ a string:

I can call:
aDateTime.Format("dd/mm/yyyy", aFormatProvider)
anInt.Format("###,###.##", aFormatProvider)
and
aDateTime = DateTime.ParseExact(aStringDate, "dd/mm/yyyy", aFormatProvider)
but not
anInt.Parse(aStringInt, "###,###.##", aFormatProvider)
 
Back
Top