Archive for the ‘c#’ Category
Retrouver un numéro de semaine
public static int GetWeekNumber (DateTime dt) {
CultureInfo culture = CultureInfo.CurrentCulture;
int intWeek = culture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return intWeek;
}
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/856)
Convertir un type String en Enum
enum EngineType {
unknow,
access,
db2,
mysql,
odbc,
oledb,
oracle,
postgre,
sqlserver
}
string cnxTypeString = "mysql";
EngineType cnxTypeEngine = EngineType.unknow;
if (Enum.IsDefined(typeof(EngineType), cnxTypeString)) {
cnxTypeEngine = (EngineType) Enum.Parse(typeof(EngineType), cnxTypeString, true);
}
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/824)
Algorithme pour calculer Pâques
///
/// Algorithm for calculating the date of Easter Sunday
/// (Meeus/Jones/Butcher Gregorian algorithm)
/// http://en.wikipedia.org/wiki/Computus#Meeus.2FJones.2FButcher_Gregorian_algorithm
///
/// A valid Gregorian year
/// Easter Sunday
public static DateTime EasterDate(int year) {
int Y = year;
int a = Y % 19;
int b = Y / 100;
int c = Y % 100;
int d = b / 4;
int e = b % 4;
int f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30;
int i = c / 4;
int k = c % 4;
int L = (32 + 2 * e + 2 * i - h - k) % 7;
int m = (a + 11 * h + 22 * L) / 451;
int month = (h + L - 7 * m + 114) / 31;
int day = ((h + L - 7 * m + 114) % 31) + 1;
DateTime dt = new DateTime(year, month, day);
return dt;
}
- Lundi de Pâques : Easter Monday = Easter Sunday + 1
- Jeudi de l’Ascension : Ascension Day = Easter Sunday + 39
- Dimanche de Pentecôte : Pentecost Sunday = Easter Sunday + 49
- Lundi de Pentecôte : Pentecost Monday = Easter Sunday + 50
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/765)
Capitalization
D’après C# Regular Expressions :
using System.Text.RegularExpressions;
public class MyClass {
public static void Main() {
string text = "the quick red fox jumped over the lazy brown DOG.";
System.Console.WriteLine("text=[" + text + "]");
string result = Regex.Replace(text, @"w+", new MatchEvaluator(MyClass.CapText));
System.Console.WriteLine("result=[" + result + "]");
System.Console.ReadLine();
}
static string CapText(Match m) {
string temp = m.ToString();
temp = char.ToUpper(temp[0]) + temp.Substring(1, temp.Length - 1).ToLower();
return temp;
}
}
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/705)
Edit : commentaire de utagger :
Here’s a shorter version:
protected void Button1_Click(object sender, EventArgs e) {
Label1.Text = Regex.Replace(TextBox1.Text, @ »\b\w », new MatchEvaluator(stam));
}protected string stam(Match m) {
return m.Value.ToUpper();
}(the trick is using \b which is a 0-length match of word boundaries, including ^ and \s)
yyyymmdd to DateTime
DateTime myDate;
myDate = System.DateTime.ParseExact("20050802",
"yyyyMMdd",
System.Globalization.CultureInfo.InvariantCulture);
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/541)
DateTime to yyyymmdd
string myString = myDate.ToString("yyyyMMdd");
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/540)
Set ValueToCompare at run time
Lorsque un contrôle CompareValidator est utilisé pour contrôler une date et que la valeur de comparaison est définie côté code, il faut formatter cette date au format de date courte :
myCompareValidator.ValueToCompare = myDateTime.ToShortDateString();
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/524)
Générer une chaine aléatoire
///
/// Build a random string (for id, login, password...)
///
public static string randomString() {
int length = new Random().Next(6, 10);
return randomString(length);
}
///
/// Build a random string (for id, login, password...)
///
public static string randomString(int length) {
string tempString = Guid.NewGuid().ToString().ToLower();
tempString = tempString.Replace("-", "");
while (tempString.Length
(Publié à l'origine sur http://www.bigbold.com/snippets/posts/show/518)
Encoder une chaine en SHA1 et hexa
public static string SHA1_ComputeHexaHash (string text) {
// Gets the SHA1 hash for text
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
// Transforms as hexa
string hexaHash = "";
foreach (byte b in hash) {
hexaHash += String.Format("{0:x2}", b);
}
// Returns SHA1 hexa hash
return hexaHash;
}
(publié à l’origine sur http://www.bigbold.com/snippets/posts/show/517)
Encoder une chaine en MD5 et hexa
public static string MD5_ComputeHexaHash (string text) {
// Gets the MD5 hash for text
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(text);
byte[] hash = md5.ComputeHash(data);
// Transforms as hexa
string hexaHash = "";
foreach (byte b in hash) {
hexaHash += String.Format("{0:x2}", b);
}
// Returns MD5 hexa hash
return hexaHash;
}
(publié à l’origine sur http://www.bigbold.com/snippets/posts/show/516)
Vous devez être connecté pour poster un commentaire.