对一些字符串进行操作的类
public class StringUtil
{
private static string passWord; //加密字符串
/// <summary>
/// 判断输入是否数字
/// </summary>
/// <param name="num">要判断的字符串</param>
/// <returns></returns>
static public bool VldInt(string num)
{
#region
try
{
Convert.ToInt32(num);
return true;
}
catch { return false; }
#endregion
}
/// <summary>
/// 截取字符串函数
/// </summary>
/// <param name="str">所要截取的字符串</param>
/// <param name="num">截取字符串的长度</param>
/// <returns></returns>
static public string GetSubString(string str, int num)
{
#region
return (str.Length > num) ? str.Substring(0, num) + "..." : str;
#endregion
}
/// <summary>
/// 过滤输入信息
/// </summary>
/// <param name="text">内容</param>
/// <param name="maxLength">最大长度</param>
/// <returns></returns>
public static string InputText(string text, int maxLength)
{
#region
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
if (text.Length > maxLength)
text = text.Substring(0, maxLength);
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
return text;
#endregion
}
/// <summary>
/// 生成随机数
/// </summary>
/// <returns></returns>
static public string GenerateCheckCode()
{
#region
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
HttpContext.Current.Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
return checkCode;
#endregion
}
/// <summary>
/// 获取汉字第一个拼音
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
static public string getSpells(string input)
{
#region
int len = input.Length;
string reVal = "";
for (int i = 0; i < len; i++)
{
reVal += getSpell(input.Substring(i, 1));
}
return reVal;
#endregion
}
/// <summary>
/// 汉字编码转换,解决IE地址栏中文
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
static public string Strencode(string input)
{
#region
return System.Web.HttpUtility.UrlEncode(InputText(input,100));
#endregion
}
static public string getSpell(string cn)
{
#region
byte[] arrCN = Encoding.Default.GetBytes(cn);
if (arrCN.Length > 1)
{
int area = (short)arrCN[0];
int pos = (short)arrCN[1];
int code = (area << 8) + pos;
int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
for (int i = 0; i < 26; i++)
{
int max = 55290;
if (i != 25) max = areacode[i + 1];
if (areacode[i] <= code && code < max)
{
return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
}
}
return "?";
}
else return cn;
#endregion
}
/// <summary>
/// 半角转全角
/// </summary>
/// <param name="BJstr"></param>
/// <returns></returns>
static public string GetQuanJiao(string BJstr)
{
#region
char[] c = BJstr.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
if (b.Length == 2)
{
if (b[1] == 0)
{
b[0] = (byte)(b[0] - 32);
b[1] = 255;
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
string strNew = new string(c);
return strNew;
#endregion
}
/// <summary>
/// 全角转半角
/// </summary>
/// <param name="QJstr"></param>
/// <returns></returns>
static public string GetBanJiao(string QJstr)
{
#region
char[] c = QJstr.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
if (b.Length == 2)
{
if (b[1] == 255)
{
b[0] = (byte)(b[0] + 32);
b[1] = 0;
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
string strNew = new string(c);
return strNew;
#endregion
}
#region 加密的类型
/// <summary>
/// 加密的类型
/// </summary>
public enum PasswordType
{
SHA1,
MD5
}
#endregion
/// <summary>
/// 字符串加密
/// </summary>
/// <param name="PasswordString">要加密的字符串</param>
/// <param name="PasswordFormat">要加密的类别</param>
/// <returns></returns>
static public string EncryptPassword(string PasswordString, string PasswordFormat)
{
#region
switch (PasswordFormat)
{
case "SHA1":
{
passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
break;
}
case "MD5":
{
passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");
break;
}
default:
{
passWord = string.Empty;
break;
}
}
return passWord;
#endregion
}
}