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

编程入门 行业动态 更新时间:2024-10-25 15:22:46
本文介绍了索引和长度必须引用一个位置的子字符串误差范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个这样的字符串: 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.

返回值结果类型: 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.

更多推荐

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

本文发布于:2023-11-09 23:06:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:范围内   误差   字符串   长度   索引

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!