Posts Tagged ‘c#’

January 16, 2008 1

C# Coding Standards and Best Programming Practices

By J in Uncategorized

I was reading a best practices document created by the DotNetSpider team which covered many aspects of programming in .NET. I was really impressed by it and I think its a very good reference document. So I’ve decided to make a series of posts where I’ll post sections of their document covering a specific topic.

Tags: ,

January 12, 2008 0

A Semi-Dynamic Sitemap Solution

By J in Uncategorized

I was creating a website which had pages with multiple views. I wanted a fairly simple sitemap solution, without creating any custom sitemap provider, which could show the current view name in the breadcrumb trail like this –
Home >> Parent Page >> Current Page >> Current View 1
Home >> Parent Page >> Current Page [...]

Tags: ,

January 5, 2008 3

How to extract URLs (href property) from HTML

By J in Uncategorized

protected ArrayList getURL(string txtIn)
{
ArrayList outURL = new ArrayList();
Regex r = new Regex(“href\\s*=\\s*(?:(?:\\\”(?<url>[^\\\"]*)\\\”)|(?<url>[^\\s]* ))”);
MatchCollection mc1 = r.Matches(txtIn);

foreach (Match m1 in mc1)
{
foreach (Group g in m1.Groups)
[...]

Tags: , ,

December 27, 2007 1

How to remove all special characters from a string

By J in Uncategorized

protected string StripSpecChars(string txtIn)
{
string txtOut = Regex.Replace(txtIn, @”[^\w\.@-]“, “”).Trim();
return txtOut;
}

Tags: ,

December 20, 2007 1

How to remove commonly occuring English words from a string

By J in Uncategorized

I’m using this function to filter common words out of a search query.

protected string removeCommonWords(string sourceStr)
{
string[] seperator = { ” ” };
string[] ignoreWords = { “a”, “all”, “am”, “an”, “and”, “any”, “are”, “as”,
“at”, “be”, “but”, “can”, “did”, “do”, “does”, [...]

Tags: ,

November 13, 2007 0

Strip out HTML tags using RegEx

By J in Uncategorized

This code will strip out all the HTML tags and truncate the text to 4 lines.

public static string TruncateText(string txtIn, int newLength)
{
string txtOut = txtIn;
string pattern = @”<(.|\n)*?>”;

//Strip out HTML tags
if (Regex.IsMatch(txtIn, pattern, RegexOptions.None))
[...]

Tags: , ,

October 15, 2007 0

Recursive Goodness – II

By J in Uncategorized

Find a control within a control recursively:

protected void Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
[...]

Tags:

October 15, 2007 0

Recursive Goodness – I

By J in Uncategorized

Check to see if all the controls within a specific control are valid:

protected bool checkIsValid(Control c)
{
bool result = true;
foreach (Control child in c.Controls)
{

if (child is BaseValidator && !((IValidator)child).IsValid)
[...]

Tags: