【问题标题】:How to parse specific characters of a string to integer?如何将字符串的特定字符解析为整数?
【发布时间】:2012-06-27 11:16:51
【问题描述】:
string abc = "07:00 - 19:00"
x  = int.Parse(only first two characters) // should be 7
y  = int.Parse(only 9th and 10th characters) // should be 19

请问我怎么能这么说?

【问题讨论】:

    标签: c# winforms string parsing integer


    【解决方案1】:

    使用字符串类的 Substring 方法提取所需的字符集。

    string abc = "07:00 - 19:00";
    x  = int.Parse(abc.Substring(0,2)); // should be 7
    y  = int.Parse(abc.Substring(8,2)); // should be 19
    

    【讨论】:

      【解决方案2】:

      没有采用范围的int.Parse,因此:

      • 编写您自己的解析器 (yeuch)
      • 先使用Substring,然后在子字符串上使用int.Parse

      所以:

      x = int.Parse(abc.Substring(0,2));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-18
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-10
        • 1970-01-01
        相关资源
        最近更新 更多