Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
gives this error

System.ArgumentOutOfRangeException: 'Index and count must refer to a location within the string.'


string Intermeddiate = //Literally any news websites articles html
string Titlestart = $"<title>";
string Titleend = $"";
var matches = Regex.Matches(Intermeddiate.ToString(), "<title>(.*?)</title>");
           foreach (var item in matches)
           {


               if (TitlestartPresnt && TitleendPresent)
               {

                   string titleImpure = item.ToString();
                   string removedfront = titleImpure.Remove(0, 7);
                   string removedback = removedfront.Remove(removedfront.IndexOf(Titleend), removedfront.IndexOf(Titleend)+7);
                   Title = removedback;
                   Console.WriteLine(Title);
                  // int Titlestartindex = item.IndexOf(Titlestart);
                  //int Titleemdindex = Intermeddiate.IndexOf(Titleend);
                  // if (Titlestartindex+50 > Titleemdindex -50)
                  // {



                   //     int titlestart = Intermeddiate.IndexOf(Titlestart) + Titlestart.Length;
                   //     int titleend = Intermeddiate.IndexOf(Titleend) - 10; //make so works properly
                   //     string PureTexttitle = Intermeddiate.Substring(titlestart, titleend - titlestart);

                   //     Console.WriteLine("Title-" + PureTexttitle);

                   //     TitlesCList.Add(PureTexttitle);
                   // }
                   // else
                   // {
                   //     Console.WriteLine("No title for this article");
                   // }
               }

           }


TitlestartPresent
can be multiple occurences of titlestart and titlened
the stuff in the comments does the same thing and gives th esame error really

tried doing a check to make sure that the indexes of titlestart and titleend overlap incase there is more than 1 titlestart and titleend in the entire html

wanna cry

What I have tried:

more or less everything I can think of
Posted
Comments
Richard MacCutchan 27-Dec-19 3:35am    
Which line causes the error, and what are the values of all the variables that are used in that statement?
HamzaMcBob 27-Dec-19 5:21am    
this line provides the error
string removedback = removedfront.Remove(removedfront.IndexOf(Titleend), removedfront.IndexOf(Titleend)+7);
HamzaMcBob 27-Dec-19 5:21am    
and these are all the variables

string Intermeddiate = //Literally any news websites articles html
string Titlestart = $"<title>";
string Titleend = $"";
[no name] 27-Dec-19 11:43am    
If you're coding "magic numbers" (7, 50, 10), chances are you guessed wrong at some point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900