example of the string: alt="hello world" >" i want to get only the hello world using indexof and substring.
int indexDescription = line.IndexOf("alt=");
int indexDescriptionEnd = line.IndexOf("\" >");
string descriptionResult = line.Substring(indexDescription, indexDescriptionEnd - indexDescription);
the result is: "alt="hello world"
I can't figure out how to rid of the "alt=
I tried:
string descriptionResult = line.Substring(indexDescription + 6, indexDescriptionEnd - indexDescription);
but his give exception error:
System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. Parameter name: length'
String.IndexOf will return the index of the first occurrence of the passed string. If passed string has more than 1 character, the index is the position of the first character of that string. For your example with the string "alt=", first occurrence is at the beginning of the string, so the index is 0. You need to add the length of "alt=\""
to your indexDescription
. Something like:
int indexDescription = line.IndexOf("alt=\"") + "alt=\"".Length;
int indexDescriptionEnd = line.IndexOf("\" >");
string descriptionResult = line.Substring(indexDescription, indexDescriptionEnd - indexDescription)
Keep in mind though that this problem is very trivial and it'll only work for the assumptions you provided (i.e. all strings are exactly as you described them to be)