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

Re: ToString(string) and parsing by Michael

Michael
Thu Jan 12 11:21:33 CST 2006

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


"Jerry Shea" <JerryShea@discussions.microsoft.com> schrieb im Newsbeitrag
news:ADA4FC7D-B7B1-46B1-9822-2A93C12A36E6@microsoft.com...
> 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



Re: ToString(string) and parsing by Jerry

Jerry
Thu Jan 12 16:08:03 CST 2006

Thanks Michael but I was looking for a Parse method which understands a
string format as in IFormattable.ToString(strFormat, formatProvider);

Re: ToString(string) and parsing by Jim

Jim
Thu Jan 12 21:33:22 CST 2006

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

"Jerry Shea" <JerryShea@discussions.microsoft.com> wrote in message
news:ADA4FC7D-B7B1-46B1-9822-2A93C12A36E6@microsoft.com...
> 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



Re: ToString(string) and parsing by JerryShea

JerryShea
Thu Jan 12 22:24:02 CST 2006

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)