ASP.NET常用方法集合

1、返回字符串真实长度, 1个汉字长度为2
using System.Text;
/// <summary>
/// 返回字符串真实长度
/// </summary>
/// <param name="str">字符串</param>
/// <returns>字符串真实长度</returns>
public static int GetStringLength(string str)
{
return Encoding.Default.GetBytes(str).Length;
}

2、获得当前绝对路径
using System.Web;
/// <summary>
/// 获得当前绝对路径
/// </summary>
/// <param name="strPath">路径字符串</param>
/// <returns>当前绝对路径</returns>
public static string GetMapPath(string strPath)
{
if(HttpContext.Current != null)
{
   return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
   return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,   strPath);
}
}
3、判断指定字符串在指定字符串数组中的位置
/// <summary>
/// 判断指定字符串在指定字符串数组中的位置
/// </summary>
/// <param name="strSearch">字符串</param>
/// <param name="stringArray">字符串数组</param>
/// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param>
/// <returns>字符串在指定字符串数组中的位置, 如不存在则返回-1</returns>
public static int GetInArrayID(string strSearch, string[] stringArray, bool caseInsensetive)
{
for (int i = 0; i < stringArray.Length; i++)
{
   if (caseInsensetive)
   {
    if(strSearch.ToLower() == stringArray[i].ToLower())
    {
     return i;
    }
   }
   else
   {
    if(strSearch == stringArray[i])
    {
     return i;
    }
   }    
   
}
return -1;
}

4、分割字符串,保存为数组
/// <summary>
/// 分割字符串,保存为数组
/// </summary>
/// <param name="strContent">字符串</param>
/// <param name="strSplit">分隔字符或字符串</param>
/// <returns>结果数组</returns>
public static string[] SplitString(string strContent, string strSplit)
{
if(strContent.IndexOf(strSplit) < 0)
{
   string[] tmp = {strContent};
   return tmp;
}
return Regex.Split(strContent, @strSplit.Replace(".",@"/."), RegexOptions.IgnoreCase);
}

5、将字符串转换为简体中文或繁体中文
using Microsoft.VisualBasic;
/// <summary>
/// 转换为简体中文
/// </summary>
public static string ToSChinese(string str)
{
return Strings.StrConv(str, VbStrConv.SimplifiedChinese, 0) ;
}

/// <summary>
/// 转换为繁体中文
/// </summary>
public static string ToTChinese(string str)
{
return Strings.StrConv(str, VbStrConv.TraditionalChinese, 0);
}

6、判断由特定字符或字符串分隔的字符串中,是否包含另一字符串
/// <summary>
/// 判断由特定字符或字符串分隔的字符串中,是否包含另一字符串
/// </summary>
/// <param name="str">被判断字符串</param>
/// <param name="stringarray">可能包含被判断字符串的字符串</param>
/// <param name="strsplit">分隔字符或字符串</param>
/// <returns>true:包含,false:不包含</returns>
public static bool IsCompriseStr(string str, string stringarray, string strsplit)
{
if (stringarray == "" || stringarray == null)
   return false;

str = str.ToLower();
string[] stringArray = SplitString(stringarray.ToLower(), strsplit);
//SplintString方法参考方法4
for (int i = 0; i < stringArray.Length; i++)
{
   if(str.IndexOf(stringArray[i]) > -1)
   {
    return true;
   }
}
return false;
}

7、判断文件是否存在
/// <summary>
/// 返回文件是否存在
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>是否存在</returns>
public static bool FileExists(string filename)
{
return System.IO.File.Exists(filename);
}

8、判断文件名是否是浏览器可以直接显示的图片文件名
/// <summary>
/// 判断文件名是否为浏览器可以直接显示的图片文件名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>是否可以直接显示</returns>

public static bool IsImgFilename(string filename)
{
filename = filename.Trim();
if (filename.EndsWith(".") || filename.IndexOf(".") == -1)
{
   return false;
}
string extname = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();
return (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "bmp" || extname == "gif");
}   

9、MD5加密函数
using System.Text;
using System.Security.Cryptography;
/// <summary>
/// MD5加密函数
/// </summary>
/// <param name="str">原始字符串</param>
/// <returns>MD5结果</returns>
public static string MD5(string str)
{
byte[] b = Encoding.Default.GetBytes(str);
b = new MD5CryptoServiceProvider().ComputeHash(b);
string ret = "";
for(int i = 0; i < b.Length; i++)
   ret += b[i].ToString("x").PadLeft(2,'0');
return ret;
}

10、SHA256加密函数
using System.Security.Cryptography;
/// <summary>
/// SHA256函数
/// </summary>
/// /// <param name="str">原始字符串</param>
/// <returns>SHA256结果</returns>
public static string SHA256(string str)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result);   //返回长度为44字节的字符串
}

11、判断字符串是否是IP地址格式
/// <summary>
/// 是否为ip
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsIP(string ip)
{
return Regex.IsMatch(ip, @"^((2[0-4]/d|25[0-5]|[01]?/d/d?)/.){3}(2[0-4]/d|25[0-5]|[01]?/d/d?)$");
}

12、读写cookie操作
using System.Web;
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
   cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
   cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}

/// <summary>
/// 读cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <returns>cookie值</returns>
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
{
   return HttpContext.Current.Request.Cookies[strName].Value.ToString();
}

return "";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值