2016-12-19 1 views
-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; 
} 

답변

0

에 올 수 없습니다.

while (endposition < line.Length && !Char.IsLetterOrDigit(line[endposition]) ) 
관련 문제