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

shubham Hegdey

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 know the start of the substring so I'm taking the end of the substring as the length of the whole string like this:

 No = line.Substring(Start,End);

Here the value of Start is 39 and the length of the main string is 52 so this is the value for End.

This causes the error:

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

How do I resolve this?

Jon Skeet

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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Pulling variable length substring from middle of string

来自分类Dev

Fixing "index out of range" error within code

来自分类Dev

Java String length()和substring(int,int)是否不考虑某些字符?

来自分类Dev

Find CGPoint Location of substring in TextView

来自分类Dev

Check for a substring at the end of string

来自分类Dev

修改string.Substring

来自分类Dev

Substring depending on Delimiter in the string

来自分类Dev

String.Substring解释

来自分类Dev

splitting a string if a substring has occured

来自分类Dev

Count number of a substring repetition in a string

来自分类Dev

ORM中的substring_index函数

来自分类Dev

在PGSQL中模拟MySQL的substring_index()

来自分类Dev

替换mysql中的substring_index

来自分类Dev

在MySQL中使用substring_index

来自分类Dev

在列上运行SUBSTRING_INDEX查询

来自分类Dev

std::length_error: basic_string 将 wstring 转换为字符串

来自分类Dev

C#string.Substring()或string.Remove()

来自分类Dev

Google Sign-In for iOS: error "Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String'"

来自分类Dev

compute length string of variable with cobol

来自分类Dev

How to validate string length with Mongoose?

来自分类Dev

index:length(......):长度为0的参数

来自分类Dev

String.substring()复制基础char []值

来自分类Dev

循环内的java string.Substring StringIndexOutOfBoundsException

来自分类Dev

Delete a substring from a string with a filename in Perl

来自分类Dev

某些输入时String.Substring()崩溃

来自分类Dev

.NET 到 Delphi(string.Substring 函数)

来自分类Dev

在抛出'std :: length_error'what()实例之后调用终止终止what():basic_string :: __ S_create

来自分类Dev

如何在使用substring_index时摆脱换行空间

来自分类Dev

SUBSTRING_INDEX,带有多个定界符

Related 相关文章

  1. 1

    Pulling variable length substring from middle of string

  2. 2

    Fixing "index out of range" error within code

  3. 3

    Java String length()和substring(int,int)是否不考虑某些字符?

  4. 4

    Find CGPoint Location of substring in TextView

  5. 5

    Check for a substring at the end of string

  6. 6

    修改string.Substring

  7. 7

    Substring depending on Delimiter in the string

  8. 8

    String.Substring解释

  9. 9

    splitting a string if a substring has occured

  10. 10

    Count number of a substring repetition in a string

  11. 11

    ORM中的substring_index函数

  12. 12

    在PGSQL中模拟MySQL的substring_index()

  13. 13

    替换mysql中的substring_index

  14. 14

    在MySQL中使用substring_index

  15. 15

    在列上运行SUBSTRING_INDEX查询

  16. 16

    std::length_error: basic_string 将 wstring 转换为字符串

  17. 17

    C#string.Substring()或string.Remove()

  18. 18

    Google Sign-In for iOS: error "Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String'"

  19. 19

    compute length string of variable with cobol

  20. 20

    How to validate string length with Mongoose?

  21. 21

    index:length(......):长度为0的参数

  22. 22

    String.substring()复制基础char []值

  23. 23

    循环内的java string.Substring StringIndexOutOfBoundsException

  24. 24

    Delete a substring from a string with a filename in Perl

  25. 25

    某些输入时String.Substring()崩溃

  26. 26

    .NET 到 Delphi(string.Substring 函数)

  27. 27

    在抛出'std :: length_error'what()实例之后调用终止终止what():basic_string :: __ S_create

  28. 28

    如何在使用substring_index时摆脱换行空间

  29. 29

    SUBSTRING_INDEX,带有多个定界符

热门标签

归档