Return to Snippet

Revision: 18581
at October 2, 2009 12:32 by jasonseney


Updated Code
public static string StripXML(string source)
{
	char[] buffer = new char[source.Length];
	int bufferIndex = 0;
	bool inside = false;

	for (int i = 0; i < source.Length; i++)
	{
		char let = source[i];
		if (let == '<')
		{
			inside = true;
			continue;
		}
		if (let == '>')
		{
			inside = false;
			continue;
		}
		if (!inside)
		{
			buffer[bufferIndex] = let;
			bufferIndex++;
		}
	}
	return new string(buffer, 0, bufferIndex);
}

Revision: 18580
at October 2, 2009 12:31 by jasonseney


Initial Code
public static string Truncate(string sourceText, string ellipsis, int min, int max)
{
	//If text is shorter than preview length
	if (sourceText.Length <= max)
	{
		return sourceText; //@RETURN break out early if too short
	}

	//Grab the char at the last position allowed
	char cutOffChar = sourceText[max];

	int lastPosition = max;

	//While the last char isn't a space, cut back until we hit a space or minimum
	while (cutOffChar != ' ' && lastPosition > min)
	{
		lastPosition--;
		cutOffChar = sourceText[lastPosition];
	}

	//Crop text and add some dots
	string outText = sourceText.Substring(0, lastPosition);
	outText += ellipsis;

	return outText;
}

Initial URL

                                

Initial Description
[Credit - Sam Allen, Dot Net Perls](http://dotnetperls.com/remove-html-tags)

Initial Title
C# Strip XML/HTML from string

Initial Tags
html, xml

Initial Language
C#