-2
獲取未處理的異常:System.IndexOutOfRangeException:索引位於數組邊界之外。在寫評論的線上。該程序將分隔符添加到只知道結束位置的單詞中。在單詞嘗試添加null!=之後沒有任何內容時得到錯誤,但它沒有幫助。我明白爲什麼它發生的原因,但不能來解決C#System.IndexOutOfRangeException:索引超出數組範圍
static void Find(ref string line, string sep, out string z)
{
z = "";
string[] words = line.Split(sep.ToCharArray());
string[] NeedToBeChecked = new string[words.Length];
int a = 0;
for (int i = 0; i < words.Length; i++)
{
if (TikSkaitmenys(words[i]) == true)
{
NeedToBeChecked[a] = words[i];
a++;
}
}
string LongestWord = "";
int temp = 0;
for (int i = 0; i < NeedToBeChecked.Length; i++)
{
if (NeedToBeChecked[i] != null)
{
if (NeedToBeChecked[i].Length > temp)
{
temp = NeedToBeChecked[i].Length;
LongestWord = NeedToBeChecked[i];
}
}
}
z = LongestWord;
int[] indexarray = new int[50];
int index = 0;
int position = 0;
int endposition = 0;
string word = "";
if (LongestWord != "" && LongestWord != null)
{
do
{
index = line.IndexOf(LongestWord, index);
if (index != -1)
{
if (index != 0)
{
if ('.' == line[index - 1] || ',' == line[index - 1] || ' ' == line[index - 1])
{
position = index;
endposition = LongestWord.Length + index;
while (position != endposition)
{
word += line[position++];
}
}
}
if (index == 0)
{
position = index;
endposition = LongestWord.Length + index;
while (index != endposition)
{
word += line[index++];
}
}
index++;
}
} while (index != -1);
while (!Char.IsLetterOrDigit(line[endposition])) // get Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
{
word += line[endposition];
endposition++;
}
}
z = word;
}