Một số hàm static dưới đây là mình tổng hợp được và chia sẻ với các bạn để thuận tiện hơn trong việc lập trình, không riêng gì ASP.NET C# mà cả ngôn ngữ lập trình C# nói chung. Tùy theo trường hợp cần thiết, các bạn có thể áp dụng vào bất cứ đâu mình thích.
//Add css
public static void AddCss(string path, Page page)
{
Literal cssFile = new Literal() { Text = @"<link href=""" + page.ResolveUrl(path) + @""" type=""text/css"" rel=""stylesheet"" />" };
page.Header.Controls.Add(cssFile);
}
//Add js
public static void AddJs(string path, Page page)
{
LiteralControl jsFile = new LiteralControl("<script type='text/javascript' src='" + path + "'></script>");
page.Header.Controls.Add(jsFile);
}
//Lấy số ngẫu nhiên
public static int RandomNumber(int min, int max)
{
Random random = new Random(); return random.Next(1, 1000);
}
//Lấy ngày giờ tại Server
public static DateTime GetServerDateTime()
{
return DateTime.Now.ToUniversalTime().AddHours(7);
}
//Mã hóa thành chuỗi MD5
public static string EncryptMD5(string sToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(sToEncrypt);
// encrypt bytes
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
//Chuẩn hóa chuỗi ký tự số
public static string StandardNumber(string num)
{
num = num.Replace("`", "");
num = num.Replace("-", "");
num = num.Replace("=", "");
num = num.Replace("~", "");
num = num.Replace("!", "");
num = num.Replace("@", "");
num = num.Replace("#", "");
num = num.Replace("$", "");
num = num.Replace("%", "");
num = num.Replace("^", "");
num = num.Replace("&", "");
num = num.Replace("*", "");
num = num.Replace("(", "");
num = num.Replace(")", "");
num = num.Replace("_", "");
num = num.Replace("+", "");
num = num.Replace("[", "");
num = num.Replace("]", "");
num = num.Replace(";", "");
num = num.Replace("'", "");
num = num.Replace(",", "");
num = num.Replace(".", "");
num = num.Replace("/", "");
num = num.Replace("{", "");
num = num.Replace("}", "");
num = num.Replace(":", "");
num = num.Replace("<", "");
num = num.Replace(">", "");
num = num.Replace("?", "");
return num;
}
//Chuẩn hóa chuỗi ký tự bảo mật
public static string StandardString(string sContent)
{
sContent = sContent.Trim();
sContent = sContent.Replace("<", "");
sContent = sContent.Replace(">", "");
sContent = sContent.Replace("crack", "*****");
sContent = sContent.Replace("key", "***");
sContent = sContent.Replace("<td>", "");
sContent = sContent.Replace("</td>", "");
sContent = sContent.Replace("<tr>", "");
sContent = sContent.Replace("</tr>", "");
sContent = sContent.Replace("<table>", "");
sContent = sContent.Replace("</table>", "");
sContent = sContent.Replace("<script", "");
sContent = sContent.Replace("</script>", "");
sContent = sContent.Replace("OR", "");
sContent = sContent.Replace("ALTER", "");
sContent = sContent.Replace("DROP", "");
return sContent;
}
//Thay thế chữ cái có dấu thành không dấu
public static string RemapInternationalCharToAscii(char c)
{
string s = c.ToString();
Regex regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
string temp = s.Normalize(NormalizationForm.FormD);
return regex.Replace(temp, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
}
//Ghi lại Url thân thiện
public static string RewriteUrlFriendly(string title)
{
if (title == null) return "";
const int maxlen = 80;
int len = title.Length;
bool prevdash = false;
var sb = new StringBuilder(len);
char c;
for (int i = 0; i < len; i++)
{
c = title[i];
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
{
sb.Append(c);
prevdash = false;
}
else if (c >= 'A' && c <= 'Z')
{
// tricky way to convert to lowercase
sb.Append((char)(c | 64));
prevdash = false;
}
else if (c == ' ' || c == ',' || c == '.' || c == '/' ||
c == '\\' || c == '-' || c == '_' || c == '=')
{
if (!prevdash && sb.Length > 0)
{
sb.Append('-');
prevdash = true;
}
}
else if ((int)c >= 128)
{
int prevlen = sb.Length;
sb.Append(RemapInternationalCharToAscii(c));
if (prevlen != sb.Length) prevdash = false;
}
if (i == maxlen) break;
}
if (prevdash)
return sb.ToString().Substring(0, sb.Length - 1);
else
return sb.ToString();
}
//Viết lại chuỗi với ký tự in hoa đầu tiên
public static string UpcaseFistChar(string s)
{
// Check for empty string.
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
// Return char and concat substring.
return char.ToUpper(s[0]) + s.Substring(1);
}
//Kiểm tra chuỗi chỉ chứa ký tự số int64
public static bool IsNumericInt64(string num)
{
long n;
return Int64.TryParse(num, out n);
}
//Kiểm tra chuỗi chỉ chứa ký tự số int32
public static bool IsNumericInt32(string num)
{
int n;
return Int32.TryParse(num, out n);
}
//Phát hiện và gắn Url trong chuỗi ký tự
public static string DetectUrlText(string text)
{
var textDetect = Regex.Replace(text, @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)", "<a target='_blank' href='$1'>$1</a>");
return textDetect;
}Chúc các bạn thành công!
Bài viết liên quan
Subscribe
0 Comments
Inline Feedbacks
View all comments