More Really Useful ASP.NET C# Functions

I'm not taking credit for all these, some I found and maybe adapted over the past few months but have found them invaluable - So thought I would pass them on for everyone else to use.

Convert BBCode Function

public string BBCode(string strTextToReplace) { ////Define regex Regex regExp; ////Regex for URL tag without anchor regExp = new Regex(@"\[url\]([^\]]+)\[\/url\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$1</a>"); ////Regex for URL with anchor regExp = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$2</a>"); ////Image regex regExp = new Regex(@"\[img\]([^\]]+)\[\/img\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<img src=\"$1\" />"); ////Bold text regExp = new Regex(@"\[b\](.+?)\[\/b\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>"); ////Italic text regExp = new Regex(@"\[i\](.+?)\[\/i\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>"); ////Underline text regExp = new Regex(@"\[u\](.+?)\[\/u\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>"); ////Font size regExp = new Regex(@"\[size=([^\]]+)\]([^\]]+)\[\/size\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"font-size: $1px\">$2</span>"); ////Font color regExp = new Regex(@"\[color=([^\]]+)\]([^\]]+)\[\/color\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"color: $1\">$2</span>"); return strTextToReplace; }

Strip HTML From String Function

public string StripHTMLFromString(string htmlText) { return Regex.Replace(htmlText, "<[^>]*>", string.Empty); }

Count Words In A String Function

public int WordCount(string Text) { string tmpStr; tmpStr = Text.Replace("\t", " ").Trim(); tmpStr = tmpStr.Replace("\n", " "); tmpStr = tmpStr.Replace("\r", " "); while (tmpStr.IndexOf(" ") != -1) tmpStr = tmpStr.Replace(" ", " "); return tmpStr.Split(' ').Length; }

Add comment


 

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]