Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Dynyx / RemoveHTML.cs
Created June 4, 2012 13:19
Remove HTML tags from a string
using System.Text.RegularExpressions;
public string removeHTML(string input)
{
string pattern = @"<(.|\n)*?>";
Regex regex = new Regex(pattern, RegexOptions.Singleline);
string output = regex.Replace(input, string.Empty);
return output;
@Dynyx
Dynyx / EncryptConnectionString.cs
Created June 4, 2012 13:19
Encrypt connection string in web.config
public static class EncryptConnection
{
public static void EncryptConnectionString(bool encrypt)
{
Configuration configFile = null;
try
{
// Open the configuration file and retrieve the connectionStrings section.
configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection configSection = configFile.GetSection("connectionStrings") as ConnectionStringsSection;
@Dynyx
Dynyx / IDictionary.cs
Created June 4, 2012 13:18
Create an IDictionary for an anonymous type
public static IDictionary<string, object> ToDictionary(this object data)
{
BindingFlags publicAttributes = BindingFlags.Public | BindingFlags.Instance;
Dictionary<string, object> dictionary = new Dictionary<string, object>();
foreach (PropertyInfo property in data.GetType().GetProperties(publicAttributes))
{
if (property.CanRead)
dictionary.Add(property.Name, property.GetValue(data, null));
}
@Dynyx
Dynyx / CapDelimited.cs
Created June 4, 2012 13:17
Split a caps delimited string
Regex.Replace("ThisIsMyCapsDelimitedString", "(\\B[A-Z])", " $1")
@Dynyx
Dynyx / IsNumeric.cs
Created June 4, 2012 13:17
IsNumeric extension method
public static class StringExtensions
{
public static bool IsNumeric(this String str)
{
double temp;
return double.TryParse(str, out temp);
}
}
@Dynyx
Dynyx / Delegate.cs
Created June 4, 2012 13:16
Delegate example
delegate string DelegateTest(string str);
DelegateTest delTest = new DelegateTest(Class1.Hello);
Console.WriteLine(delTest("LOL"));
class Class1{
...
public static string Hello(string msg) {
return "Hello " + msg;
}
@Dynyx
Dynyx / StringHex.cs
Created June 4, 2012 13:15
String to hex / Hex to string
public string ConvertStringToHex(string asciiString)
{
string hex = "";
foreach (char c in asciiString)
{
int tmp = c;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}
@Dynyx
Dynyx / Countries.cs
Created June 4, 2012 13:14
Country list
namespace Sandbox
{
public class Country
{
public string Name { get; set; }
public string ThreeLetterAbbr { get; set; }
public string TwoLetterAbbr { get; set; }
}
class Program
@Dynyx
Dynyx / States.cs
Created June 4, 2012 13:14
US state list
Dictionary<String, String> stateabbrs = new Dictionary<string,string>();
stateabbrs.Add("Alabama","AL");
stateabbrs.Add("Alaska","AK");
stateabbrs.Add("Arizona","AZ");
stateabbrs.Add("Arkansas","AR");
stateabbrs.Add("California","CA");
stateabbrs.Add("Colorado","CO");
stateabbrs.Add("Connecticut","CT");
stateabbrs.Add("Delaware","DE");
stateabbrs.Add("Florida","FL");
@Dynyx
Dynyx / IsDate.cs
Created June 4, 2012 13:13
IsDate extension method
public static bool IsDate(this string sdate)
{
DateTime dt;
bool isDate = true;
try {
dt = DateTime.Parse(sdate);
}
catch {
isDate = false;