Tuesday, July 26, 2011

Using REGEX to Change Case Selectively

I had a list of generic medications where the names were capitalized. I needed to change the spelling to lower case, which is the accepted way to spell them.

However, converting the medication name ToLower was too simplistic, since certain capitals needed to be preserved:

abobotulinumtoxinA
alteplase, tPA
aspirin, ASA
avian influenza A (H5N1) virus vaccine

Here is the code I used:


private string ConvertStringToLC(string name)
{

foreach (Match m in Regex.Matches(name, @"[A-Z]{1}[a-z]+"))
{
name = name.Substring(0, m.Index ) + char.ToLower(name[m.Index]) + name.Substring(m.Index + 1);
}

return name;
}

Because I was updating a sql database column I also had to take care of strings that had an apostrophe in them, such as burow's solution.


drugNameNew = drugNameNew.Replace("'", "''");
 
One last thing. If the generic drug name is used at the start of sentence it needs to be capitalized.


drugNameGeneric = char.ToUpper(drugNameGeneric[0]) + drugNameGeneri.Substring(1);

Monday, July 11, 2011

How to Convert an Image in jpg or other Format to an Icon

To convert an image in jpg or other format to an Icon

Open the image in Ms Paint

Click on File -> Save As and

1. Choose “Save as Type” as 24-bit Bitmap(*.bmp)
and
2. name the file with an extention of .ico: TheFileName.ico

The image does not need to be resized (to 32 x 32 or other icon size)