System.ArgumentOutOfRangeException:startIndex不能大于字符串的长度 [英] System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string
问题描述
我有这个代码.我正在尝试仅检索文本第一个程序".考虑到我知道索引说25和字符串的总长度是35.
I have this code. I am trying to retrieve just the text "first program". Considering that i know the index say 25 and total length of string is 35.
string text="Hello world ! This is my first program";
Response.Write(text.SubString(25,35));
但是在运行时出现错误"System.ArgumentOutOfRangeException:startIndex不能大于字符串的长度"
But i get the error during runtime "System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string"
推荐答案
public string Substring(int startIndex, int length)
您要在第26个字符(startIndex从零开始)之后的 中使用35个字符.
You're trying to take 35 characters after the 26th character (startIndex is zero-based), which is out of range.
如果只想从第25个字符开始到字符串末尾,请使用text.SubString(24)
If you just want to get from the 25th character to the end of the string use text.SubString(24)
这篇关于System.ArgumentOutOfRangeException:startIndex不能大于字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!