-1

我在 String fromIndexString = topStiva.Substring(topStivaLength + 1, topStiva.Length); . 我表明 topStiva.Length 超出或范围。

代码如下:

字符串actiuneRezultata(字符串topStiva,字符串cuvIntr){

        int index = -1;

        String primulCharCuvIntr = cuvIntr.Substring(0, 1);

        if (primulCharCuvIntr.Equals("+") == true)
            primulCharCuvIntr = "\\" + primulCharCuvIntr;

        else
            if (primulCharCuvIntr.Equals("*") == true)
                primulCharCuvIntr = "\\" + primulCharCuvIntr;

        else
            if (primulCharCuvIntr.Equals("*") == true)
                primulCharCuvIntr = "\\" + primulCharCuvIntr;

        else
            if (primulCharCuvIntr.Equals("*") == true)
                primulCharCuvIntr = "\\" + primulCharCuvIntr;

        else
            if (primulCharCuvIntr.Equals("*") == true)
                primulCharCuvIntr = "\\" + primulCharCuvIntr;

        for (int i = 0; i < ta.Length; i++) {

            if (ta[i].CompareTo(primulCharCuvIntr) == 0) {

                index = i;
                break;
            }
        }

        int topStivaLength = topStiva.Length - 1;

        while (topStiva[topStivaLength] >= '0' && topStiva[topStivaLength] <= 9)
            topStivaLength--;

        String fromIndexString = topStiva.Substring(topStivaLength + 1, topStiva.Length);
        int fromIndex = int.Parse(fromIndexString);

        if (index < 0)
            return "Nu face parte din gramatica!";
        else
            return tabela[fromIndex][index];

    }

topStiva 的值是从调用 actiuneRezultata(stivaAPD.Peek().ToString(), another_value) 中获取的

我在 stivaAPD 中看到我在推送一些值后感到很高兴。那么为什么超出范围呢?请帮忙!

4

2 回答 2

0

如果 topStivaLength 为 -1,这(topStiva[topStivaLength]将导致异常。

如果 topStivaLength 为 0 且 topStiva 为空字符串,这(topStiva[topStivaLength]将导致异常。

下次准确指出您在哪一行得到异常。

于 2012-12-08T17:33:22.997 回答
0

The second argument to String.Substring is the length of the desired substring. You seem to be calling it as if it were instead the (exclusive) end index of the substring. Try calling the following so that your length is equal to the distance to the end index of the string.

topStiva.Substring(topStivaLength+1, topStiva.Length - (topStivaLength+1));
于 2012-12-08T17:34:43.060 回答