Check, if string is convertable to another type

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

Hello,

maybe this is an easy one:
Is there any way to check, if a string value can be
converted into another type (e.g. DateTime, Integer)
without using exceptions (try/catch)?

Thanks in advance,
Daniel
 
in c#, not really - although some types do have a .TryParse() function.

If you wish to, you can reference Microsoft.VisualBasic, and use the
IsNumeric, IsDate, etc functions defined there. However, some of those have
lots of code in them, and my not actually be more performant than try/catch.

Lastly, you could use a compiled regular expression to match the data, which
(After the first time) will probably be faster than either other method.

-Philip
 
Philip Rieck said:
in c#, not really - although some types do have a .TryParse() function.

If you wish to, you can reference Microsoft.VisualBasic, and use the
IsNumeric, IsDate, etc functions defined there. However, some of those have
lots of code in them, and my not actually be more performant than try/catch.

Lastly, you could use a compiled regular expression to match the data, which
(After the first time) will probably be faster than either other method.

If you're interested in performance and will be getting a lot of "bad
data", you should also consider a first-pass method which might (say)
look at each character in the string to make sure it's appropriate.
I've found that this kind of thing can be far faster at filtering out a
lot of obviously bad candidates and then using an "industrial
strength" technique on the rest than just going straight to the heavy
stuff.
 
Back
Top