StringHelper

public class StringHelper
{
///


/// 获取Guid字符串值
///

///
public static string GetGuidString()
{
return Guid.NewGuid().ToString("N");
}

///


/// 删除字符串结尾的指定字符后的字符
///

public static string DelLastChar(string str, string strchar)
{
return str.Substring(0, str.LastIndexOf(strchar, StringComparison.Ordinal));
}

///


/// 把字符串按照指定分隔符装成 List 去除重复
///

///
///
///
public static List GetSubStringList(string oStr, char sepeater)
{
var ss = oStr.Split(sepeater);
return ss.Where(s => !string.IsNullOrEmpty(s) && s != sepeater.ToString(CultureInfo.InvariantCulture)).ToList();
}

///


/// 得到字符串长度,一个汉字长度为2
///

/// 参数字符串
///
public static int StrLength(string inputString)
{
var ascii = new ASCIIEncoding();
var tempLen = 0;
var s = ascii.GetBytes(inputString);
foreach (var t in s)
{
if (t == 63)
tempLen += 2;
else
tempLen += 1;
}
return tempLen;
}

///


/// 截取指定长度字符串
///

/// 要处理的字符串
/// 指定长度
/// 返回处理后的字符串
public static string CutString(string inputString, int len)
{
var isShowFix = false;
if (len % 2 == 1)
{
isShowFix = true;
len--;
}
var ascii = new ASCIIEncoding();
var tempLen = 0;
var tempString = "";
var s = ascii.GetBytes(inputString);
for (var i = 0; i < s.Length; i++)
{
if (s[i] == 63)
tempLen += 2;
else
tempLen += 1;

try
{
tempString += inputString.Substring(i, 1);
}
catch
{
break;
}

if (tempLen > len)
break;
}

var mybyte = Encoding.Default.GetBytes(inputString);
if (isShowFix && mybyte.Length > len)
tempString += "…";
return tempString;
}

///


/// 生成指定长度随机字符串(默认为6)
///

///
///
public static string GetRandomString(int codeCount = 6)
{
var str = string.Empty;
var rep = 0;
var num2 = DateTime.Now.Ticks + rep;
rep++;
var random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
for (var i = 0; i < codeCount; i++)
{
char ch;
var num = random.Next();
if ((num % 2) == 0)
{
ch = (char)(0x30 + ((ushort)(num % 10)));
}
else
{
ch = (char)(0x41 + ((ushort)(num % 0x1a)));
}
str = str + ch;
}
return str;
}

#region 获取时间戳
///


/// 获取当前时间戳
///

/// 为真时获取10位时间戳,为假时获取13位时间戳.
///
public static string GetTimeStamp(bool bflag = true)
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string ret = string.Empty;
if (bflag)
ret = Convert.ToInt64(ts.TotalSeconds).ToString();
else
ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();

return ret;
}
///


/// 获取时间戳
///

///
public static string GetTimeStamp()
{
return GetTimeStamp(DateTimeKind.Utc);
}
public static string GetTimeStamp(DateTimeKind kind)
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}
#endregion

#region 压缩string,用于压缩html
///


/// 过滤html
///

///
///
public static string FilterHTML(string HTMLStr)
{
if (!string.IsNullOrEmpty(HTMLStr))
return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>| ", "");
else
return "";
}
///
/// 压缩string,用于压缩html
///

///
///
public static string Compress(string text)
{
Regex reg = new Regex(@"\s*(]+[^>]*>)\s+(]+[^>]*>)\s*");
text = reg.Replace(text, m => m.Groups[1].Value + m.Groups[2].Value);
reg = new Regex(@"(?<=>)\s|\n|\t(?=<)");
text = reg.Replace(text, string.Empty);
return text;
}
#endregion

#region 从list 转换成字符串类型

///


/// 从list-string 转换成字符串类型
///

///
/// 分隔符
///
public static string ListStringToString(List list, string split = null)
{
string value = string.Empty;
if (list != null)
{
foreach (var val in list)
{
value += val + (split == null ? "," : split);
}
value = value.TrimEnd(split.ToCharArray());
}
return value;
}

///


/// 从list-int 转换成字符串类型
///

///
/// 分隔符
///
public static string ListIntToString(List list, string split = null)
{
string value = string.Empty;
if (list != null)
{
foreach (int val in list)
{
value += val + (split == null ? "," : split);
}
value = value.TrimEnd(split.ToCharArray());
}
return value;
}

#endregion

#region 格式验证

public static bool IsEmail(string Email) {
String strExp = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
Regex r = new Regex(strExp);
Match m = r.Match(Email);
return m.Success;
}

#endregion


#region 字符串、Unicode码转换

///


/// 字符串转Unicode码
///

/// The to unicode.
/// Value.
private string StringToUnicode(string value)
{
byte[] bytes = Encoding.Unicode.GetBytes(value);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < bytes.Length; i += 2)
{
// 取两个字符,每个字符都是右对齐。
stringBuilder.AppendFormat("u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
}
return stringBuilder.ToString();
}

///


/// Unicode转字符串
///

/// The to string.
/// Unicode.
private string UnicodeToString(string unicode)
{
string resultStr = "";
string[] strList = unicode.Split('u');
for (int i = 1; i < strList.Length; i++)
{
resultStr += (char)int.Parse(strList[i], System.Globalization.NumberStyles.HexNumber);
}
return resultStr;
}
#endregion

#region ASCII码、中文字符串互转
///


/// 含中文字符串转ASCII
///

///
///
public static string Str2ASCII(String str)
{
try
{
//这里我们将采用2字节一个汉字的方法来取出汉字的16进制码
byte[] textbuf = Encoding.Default.GetBytes(str);
//用来存储转换过后的ASCII码
string textAscii = string.Empty;

for (int i = 0; i < textbuf.Length; i++)
{
textAscii += textbuf[i].ToString("X");
}
return textAscii;
}
catch (Exception ex)
{
}
return "";
}

///


/// ASCII转含中文字符串
///

/// ASCII字符串
///
public static string ASCII2Str(string textAscii)
{
try
{
int k = 0;//字节移动偏移量

byte[] buffer = new byte[textAscii.Length / 2];//存储变量的字节

for (int i = 0; i < textAscii.Length / 2; i++)
{
//每两位合并成为一个字节
buffer[i] = byte.Parse(textAscii.Substring(k, 2), System.Globalization.NumberStyles.HexNumber);
k = k + 2;
}
//将字节转化成汉字
return Encoding.Default.GetString(buffer);
}
catch (Exception ex)
{
}
return "";
}

#endregion


}

你可能感兴趣的:(StringHelper)