索引和长度必须引用一个位置的子字符串误差范围内 [英] Index and length must refer to a location within the string error in substring

查看:262
本文介绍了索引和长度必须引用一个位置的子字符串误差范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串: 2899 8761 014零点00分00秒13年6月3日9时35 G918884770707 。我必须采取的子 G918884770707 从这个给定的字符串。我知道子,所以我走的子字符串的结束作为整个字符串这样的长度的启动:

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);

下面开始的值为 39 和主要字符串的长度 52 所以这是结束值

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

这将导致错误:

索引和长度必须引用一个位置的子字符串误差范围内。

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

我如何解决此问题?

推荐答案

您已经误解了参数的 子串 - 他们AREN T开始和结束(因为它们是在Java中),他们开始和长度

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

所以,你想:

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



从文档:

From the docs:

参数
的startIndex 的结果
类型: System.Int32 结果
在此实例中的子字符串的从零开始的起始字符位置。

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

长度的结果
键入: System.Int32 结果
的子串的字符数

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

< STRONG>返回值结果
类型: System.String 结果
一个字符串,它相当于长度的字符串长度在这种情况始于的startIndex,或空如果startIndex等于该实例的长度,长度为零。

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.

这篇关于索引和长度必须引用一个位置的子字符串误差范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆