Index and length must refer to a location within the string error in substring

后端 未结 1 512
醉酒成梦
醉酒成梦 2021-02-10 00:09

I have a string like this: 2899 8761 014 00:00:00 06/03/13 09:35 G918884770707. I have to take the substring G918884770707 from this given string. I k

相关标签:
1条回答
  • 2021-02-10 01:01

    You've misunderstood the parameters to Substring - they aren't start and end (as they are in Java), they're start and length.

    So you want:

    No = line.Substring(Start, End - Start);
    

    From the docs:

    Parameters startIndex
    Type: System.Int32
    The zero-based starting character position of a substring in this instance.

    length
    Type: System.Int32
    The number of characters in the substring.

    Return Value
    Type: System.String
    A string that is equivalent to the substring of length length that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance and length is zero.

    Always, always read the documentation - particularly if you're getting an exception that you don't understand.

    0 讨论(0)
提交回复
热议问题