Thursday, September 29, 2011

Manipulate object property using Lambda/LINQ

Code:
ShowEmployeeDelegate d = () => { tMessage.Text = "Nelson"; };  
d.Invoke();
Where ShowEmployeeDelegate is a delegate.

Tuesday, September 27, 2011

Remove Open/Close Square Brackets in a sentence:

Data: [Posted Friday September 23 2011]
Output: Posted Friday September 23 2011
Solution:
Code:
string date = Regex.Replace(date, "[\\[\\]]", string.Empty);  
date = Regex.Replace(date, @"[\[\]]", string.Empty);  

Replace character based on specific index

Here's an example snippet that will replace a character
based from a specific index.
Source: DreamInCode
To use it in your program do it like this:
Code:
  // Replaces a character in a string with the character specified.  
  void replaceCharWithChar(ref string text, int index, char charToUse)  
  {  
    char[] tmpBuffer = text.ToCharArray();  
    buffer[index] = charToUse;  
    text = new string(tmpBuffer);  
  }  

  //IN YOUR MAIN MODULE/FUNCTION.  
  //money has comma, example: $2,000, 4 BR, 180 m²   
  //LandPriceBedroom = "$120, 2 BR, 90 m²";  
  //IF COMMA OCCURS MORE THAN TWO, REPLACE THE FIRST  
  //COMMA WITH SPACE. SINCE IT COULD BE A MONEY W/  
  //THOUSANDS SEPARATOR  
  if (LandPriceBedroom.Split(',').Length > 3)   
  {  
    int firstIndexComma = LandPriceBedroom.IndexOf(",");  
    replaceCharWithChar(ref LandPriceBedroom, firstIndexComma, ' ');  
  }

Monday, September 12, 2011

Regex remove string with a punctuation beside it in a sentence!

Assuming in a sentence, you want to remove the word apply now in any case. This word has a punctuation beside it.
Example:
Apply now!
Apply now.
APPLY NOW!
APPLY NOW.
apply Now!
apply Now.

In a sentence:
We are in need of VB.NET Developers. Apply Now!
We are in need of VB.NET Developers. Apply now!
We are in need of VB.NET Developers. APPLY NOW!
We are in need of VB.NET Developers. apply now!
To remove, just use the code below:
Code:
description = Regex.Replace(description, "apply now[!.]",   string.Empty,RegexOptions.IgnoreCase).Trim();

Note: I'm not a Regex expert.

Remove dotted lines or whitespaces in Visual Studio 2010

Use:
1. ctrl r+w
or
2. ctrl e+s

Uncompress gzip response using WebClient

In a scenario where you want to download xml data using webclient, the response from firefox or any browser will display the xml file correctly. However, using a web request/webclient.downloadstring to download the xml file, the response is somewhat corrupted or in hashed form.

The response headers are the following:
1. Content-Type - application/xml
2. Content-Encoding - gzip

As you can see, the content encoding is of type gzip. The solution is to override the web request method to something like this:
Code:
  class DecompressGzipResponse : WebClient  
    {  
      protected override WebRequest GetWebRequest(Uri address)  
      {  
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);  
        request.AutomaticDecompression = DecompressionMethods.GZip   
                 | DecompressionMethods.Deflate;  
        return request;  
      }  
    }  
    //to use this in the program  
   DecompressGzipResponse client = new DecompressGzipResponse ();  
          ListingSource = client.DownloadString  
      ("http://www.boston.com/multimedia/products/realestate/listings.xml");

Reference: GZIP and HTTP Response

Thursday, September 8, 2011

Exponent to Decimal Notation

Code:
string land_area = "1.21445e-007".ToUpper();  
decimal dec = decimal.Parse(land_area, System.Globalization.NumberStyles.Float); 

Tuesday, September 6, 2011

Count number of string occurrences in C#

Code:
int count_li = tempListSource.Split(new string[]{"<li>"},         StringSplitOptions.RemoveEmptyEntries).Length - 1;

Thursday, September 1, 2011

Reset Seed in MySQL Table

ALTER TABLE tblProduct AUTO_INCREMENT = 1;
Cheers!