public abstract class StringTool
{
private static byte[] Key = { 156, 230, 245, 210, 243, 156, 142, 189 };
private static byte[] IV = { 210, 250, 243, 213, 201, 235, 223, 215 };
private static MemoryStream ms = null;
private static DESCryptoServiceProvider provider = null;
private static ICryptoTransform ctf = null;
private static CryptoStream cs = null;
private static byte[] btyIn = null;
///
/// 字符串加密
///
/// 要加密的字符串
/// 加密后的字符串
public static string Encryptor(string strSource)
{
btyIn = Encoding.Default.GetBytes(strSource);
provider = new DESCryptoServiceProvider();
provider.Key = Key;
provider.IV = IV;
ctf = provider.CreateEncryptor();
ms = new MemoryStream();
cs = new CryptoStream(ms, ctf, CryptoStreamMode.Write);
cs.Write(btyIn, 0, btyIn.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
///
/// 字符串解密
///
/// 要解密的字符串
/// 解密后的字符串
public static string Decryptor(string strSource)
{
btyIn = Convert.FromBase64String(strSource);
provider = new DESCryptoServiceProvider();
provider.Key = Key;
provider.IV = IV;
ctf = provider.CreateDecryptor();
ms = new MemoryStream(btyIn, 0, btyIn.Length);
cs = new CryptoStream(ms, ctf, CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cs, Encoding.Default);
return reader.ReadToEnd();
}
///
/// 获取Hash码
///
/// 邮箱号
/// 邮箱的Hash码
public static string GetHash(string strSource)
{
int index = strSource.GetHashCode().ToString().IndexOf('-');
if (index != -1)
{
return strSource.GetHashCode().ToString().Substring(index + 1);
}
else
{
return strSource.GetHashCode().ToString();
}
}
///
/// 判断字符串是否为正整数
///
/// 字符串
///
public static bool IsInt(string inString)
{
Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
return regex.IsMatch(inString.Trim());
}
///
/// 将List或DataTable转为json格式
///
/// List或DataTable
/// json格式字符串
public static string GetJson(object obj)
{
if (obj.GetType().Equals(typeof(DataTable)))
{
DataTable dt = (DataTable)obj;
if (dt != null && dt.Rows.Count > 0)
{
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.Append("{");
jsonBuilder.Append("ds");
jsonBuilder.Append(":[");
for (int i = 0; i < dt.Rows.Count; i++)
{
jsonBuilder.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
jsonBuilder.Append(dt.Columns[j].ColumnName);
jsonBuilder.Append(":'");
jsonBuilder.Append(dt.Rows[i][j].ToString());
jsonBuilder.Append("',");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("},");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("]");
jsonBuilder.Append("}");
return jsonBuilder.ToString();
}
else
{
return "{ds:{}}";
}
}
else
{
JavaScriptSerializer servializer = new JavaScriptSerializer();
return servializer.Serialize(obj);
}
}
///
/// 对List进行随机排序
///
/// 需要排序的集合
/// 随机排序后的集合
public static IList RandomSortList(IList ListT)
{
Random random = new Random();
IList newlist = new List();
foreach (T item in ListT)
{
newlist.Insert(random.Next(newlist.Count), item);
}
return newlist;
}
///
/// 将图片路径替换为所需要大小的缩略图路径
///
/// 原图片路径
/// 缩略图尺寸宽:200,160,128,80,40
/// 缩略图尺寸高:200,120,128,80,40
/// 缩略图路径
public static string GetThumbnails(string photoUrl, int photoSizeWidth, int photoSizeHeight)
{
if (photoUrl == null || photoUrl == "" || photoUrl.IndexOf('.') < 0)
{
return photoUrl;
}
photoUrl = photoUrl.Substring(0, photoUrl.LastIndexOf('.'));
return photoUrl + "_" + photoSizeWidth + "_" + photoSizeHeight + ".jpg";
}
///
/// 将状态字符串中的表情转为图片
///
/// 状态字符串
///
public static string GetFaceStr(string str)
{
if (str == null || str=="")
{
return str;
}
//如无表情和图片直接返回原串
if (str.IndexOf('[') < 0 && str.IndexOf(" {
return str;
}
//如有图片规定图片大小
if (str.IndexOf("= 0)
{
str = str.Replace(" }
//获取所有表情图片路径XML
string filename = Runtime.GetStartupPath(InstanceType.Web) + "resources/xml/Images.xml";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(filename);
XmlNode node = xmldoc.DocumentElement;
foreach (XmlNode current in node.ChildNodes)
{
str = str.Replace(current.ChildNodes[0].InnerText, "");
}
return str;
}
public static string PlaceJson(DataTable Table)
{
string json = "{\"MAPC_NAME\":\"" + Table.Rows[0]["MAPC_NAME"].ToString() + "\",\"MAPC_ADDRES\":\"" + Table.Rows[0]["MAPC_ADDRES"].ToString() + "\",\"CT_TYPE\":\"" + Table.Rows[0]["CT_TYPE"].ToString() + "\",\"MAPC_TEL\":\"" + Table.Rows[0]["MAPC_TEL"].ToString() + "\",\"MAPC_IMAGE\":\"" + Table.Rows[0]["CPI_SRC"].ToString() + "\"}";
return json;
}
///
/// 截取字符串
///
/// 传入字符串(汉字,全角字母,半角字母,数字)
/// 截取字符串长度
///
public static string GetSubString(string str, string length)
{
string temp = str;
int j = 0;
int k = 0;
for (int i = 0; i < temp.Length; i++)
{
if (System.Text.RegularExpressions.Regex.IsMatch(temp.Substring(i, 1), @"[\u4e00-\u9fa5]+") || System.Text.RegularExpressions.Regex.IsMatch(temp.Substring(i, 1), @"[\uFF00-\uFFFF]+"))
{
j += 2;
}
else
{
j += 1;
}
if (j <= Convert.ToInt32(length))
{
k += 1;
}
if (j > Convert.ToInt32(length))
{
return temp.Substring(0, k) + "..";
}
}
return temp;
}
///
/// 过滤html中的非法字符
///
/// 要过滤的字符串
/// 过滤后的字符串
public static string FilterHtmlString(string Htmlstring)
{
if (Htmlstring == null)
{
return "";
}
else
{
//特殊的字符
Htmlstring = Htmlstring.Replace("<", "");
Htmlstring = Htmlstring.Replace(">", "");
Htmlstring = Htmlstring.Replace("*", "");
Htmlstring = Htmlstring.Replace("-", "");
Htmlstring = Htmlstring.Replace("?", "");
Htmlstring = Htmlstring.Replace(",", "");
Htmlstring = Htmlstring.Replace("/", "");
Htmlstring = Htmlstring.Replace("\"", "");
Htmlstring = Htmlstring.Replace(";", "");
Htmlstring = Htmlstring.Replace("*/", "");
Htmlstring = Htmlstring.Replace("\r\n", "");
Htmlstring = Htmlstring.Replace("\n", "");
Htmlstring = Htmlstring.Replace("\\n", "");
Htmlstring = Htmlstring.Replace("\r", "");
Htmlstring = Htmlstring.Replace("\\r", "");
Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"\s", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
}
}