Fastest way to check if a string can be parsed

前端 未结 5 1523
礼貌的吻别
礼貌的吻别 2021-01-01 14:38

I am parsing CSV files to lists of objects with strongly-typed properties. This involves parsing each string value from the file to an IConvertible type (

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 15:22

    How about constructing a regular expression for each type and applying it to the string before calling Parse? You'd have to build the regular expression such that if the string doesn't match, it wouldn't parse. This would be a little slower if the string parses since you'd have to do the regex test, but it would be way faster if it doesn't parse.

    You could put the regex strings in a Dictionary, which would make determining which regex string to use simple.

提交回复
热议问题