首页 > 解决方案 > C#超出范围的子字符串

问题描述

我尝试像这样填充字符串[]:

public string Reception = "";
public void SaveData()
{
 int lengthReception = Reception.Length; //i got 19
 string[] Data = new Data[lengthReception];
 for(int i = lengthReception, i>0, i--)
 {
  Data[i] = Reception.Substring(i,1); //i try to store one charactere by case
 }                                    //but i got error message ArgumentOutOfRangeException
}

所以我想逐个存储一个字符。根据 doc microsoft,我的 Substring 看起来不错?!

谢谢你的帮助!

标签: c#substring

解决方案


如果Reception.Length是,则索引或更高19处没有字符。19

这有效:

public void SaveData()
{
    int lengthReception = Reception.Length; //= 19
    string[] Data = new string[lengthReception];
    for (int i = lengthReception - 1; i >= 0; i--)
    {
        Data[i] = Reception.Substring(i, 1);
    }
}

推荐阅读