Code:
string RemoveHTML(string text)
{
string cleanString = text;
//Iterate through the whole text as long as it have '<' and '>'
while (cleanString.Contains('<') && cleanString.Contains('>'))
{
//Getting the indices of '<' and '>'.
int openIndx = cleanString.IndexOf('<');
int closeIndx = cleanString.IndexOf('>');
//Surrounding the '<' and '>' with '^' and '~' respectively.
cleanString = cleanString.Insert(openIndx, "^");
cleanString = cleanString.Insert(closeIndx + 2, "~");
//Getting the indices of '^' and '~'.
openIndx = cleanString.IndexOf('^');
closeIndx = cleanString.IndexOf('~');
/*Clean all the text between '^' and '~'. This is meant in order to make a place holder in the text that will be replaced with a space.*/
cleanString = cleanString.Remove(openIndx , closeIndx - openIndx + 1);
//Replacing the resulting "^~" placeholder with a space.
cleanString = cleanString.Replace("^~", " ");
}
//Cleaning the text from "\n".
cleanString = cleanString.Replace("\n", " ");
//Cleaning the text from heading and trailing spaces.
cleanString = cleanString.TrimStart().TrimEnd();
return cleanString; //Return the all clean text.
}
Usage:
string cleanText = RemoveHTML("Your Text with html goes here");
I hope this helps, Silver Geeks. Let me know if you have any comments. :)
<>
ReplyDelete