Error while reassembling a string. Size is out

0

I can not complete this list, as it is:

foreach (var item in text)
            {
                string[] linha = item.Split('\n');
                foreach (var i in linha)
                {
                    string nova_string = i.Substring(0, i.LastIndexOf("\r"));
                    lista.Add(nova_string);                    
                }                
            }

You're giving this error:

  

Length can not be less than zero. Parameter name: length

    
asked by anonymous 01.03.2016 / 18:24

1 answer

3

The expression i.LastIndexOf("\r") is not finding the searched character. When the string is searched for, the return of this method is -1, as documentation . So you can only use the result of it if you can guarantee that what you are looking for exists. You need to use a if command or the ? : conditional operator to decide what to do, or what result to return when the text does not exist.

    
01.03.2016 / 18:31