zoukankan      html  css  js  c++  java
  • StringHelper

    public class StringHelper
    {
    /// <summary>
    /// 获取Guid字符串值
    /// </summary>
    /// <returns></returns>
    public static string GetGuidString()
    {
    return Guid.NewGuid().ToString("N");
    }

    /// <summary>
    /// 删除字符串结尾的指定字符后的字符
    /// </summary>
    public static string DelLastChar(string str, string strchar)
    {
    return str.Substring(0, str.LastIndexOf(strchar, StringComparison.Ordinal));
    }

    /// <summary>
    /// 把字符串按照指定分隔符装成 List 去除重复
    /// </summary>
    /// <param name="oStr"></param>
    /// <param name="sepeater"></param>
    /// <returns></returns>
    public static List<string> GetSubStringList(string oStr, char sepeater)
    {
    var ss = oStr.Split(sepeater);
    return ss.Where(s => !string.IsNullOrEmpty(s) && s != sepeater.ToString(CultureInfo.InvariantCulture)).ToList();
    }

    /// <summary>
    /// 得到字符串长度,一个汉字长度为2
    /// </summary>
    /// <param name="inputString">参数字符串</param>
    /// <returns></returns>
    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;
    }

    /// <summary>
    /// 截取指定长度字符串
    /// </summary>
    /// <param name="inputString">要处理的字符串</param>
    /// <param name="len">指定长度</param>
    /// <returns>返回处理后的字符串</returns>
    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;
    }

    /// <summary>
    /// 生成指定长度随机字符串(默认为6)
    /// </summary>
    /// <param name="codeCount"></param>
    /// <returns></returns>
    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 获取时间戳
    /// <summary>
    /// 获取当前时间戳
    /// </summary>
    /// <param name="bflag">为真时获取10位时间戳,为假时获取13位时间戳.</param>
    /// <returns></returns>
    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;
    }
    /// <summary>
    /// 获取时间戳
    /// </summary>
    /// <returns></returns>
    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
    /// <summary>
    /// 过滤html
    /// </summary>
    /// <param name="HTMLStr"></param>
    /// <returns></returns>
    public static string FilterHTML(string HTMLStr)
    {
    if (!string.IsNullOrEmpty(HTMLStr))
    return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>|&nbsp;", "");
    else
    return "";
    }
    /// <summary>
    /// 压缩string,用于压缩html
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string Compress(string text)
    {
    Regex reg = new Regex(@"s*(</?[^s/>]+[^>]*>)s+(</?[^s/>]+[^>]*>)s*");
    text = reg.Replace(text, m => m.Groups[1].Value + m.Groups[2].Value);
    reg = new Regex(@"(?<=>)s| | (?=<)");
    text = reg.Replace(text, string.Empty);
    return text;
    }
    #endregion

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

    /// <summary>
    /// 从list-string 转换成字符串类型
    /// </summary>
    /// <param name="list"></param>
    /// <param name="split">分隔符</param>
    /// <returns></returns>
    public static string ListStringToString(List<string> 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;
    }

    /// <summary>
    /// 从list-int 转换成字符串类型
    /// </summary>
    /// <param name="list"></param>
    /// <param name="split">分隔符</param>
    /// <returns></returns>
    public static string ListIntToString(List<int> 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码转换

    /// <summary>
    /// 字符串转Unicode码
    /// </summary>
    /// <returns>The to unicode.</returns>
    /// <param name="value">Value.</param>
    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();
    }

    /// <summary>
    /// Unicode转字符串
    /// </summary>
    /// <returns>The to string.</returns>
    /// <param name="unicode">Unicode.</param>
    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码、中文字符串互转
    /// <summary>
    /// 含中文字符串转ASCII
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    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 "";
    }

    /// <summary>
    /// ASCII转含中文字符串
    /// </summary>
    /// <param name="textAscii">ASCII字符串</param>
    /// <returns></returns>
    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


    }

  • 相关阅读:
    RecycleView使用心得【2】
    URL解析
    CSS 动画总结
    包含块 width 和 height 值的总结
    JS 获取页面大小
    常见跨域方法原理及其用例
    CSS 计数器
    JS 对象总结
    JS 原型以及原型链
    关于未能找到源文件“.NETFramework,Version=v4.0.AssemblyAttributes.cs”问题
  • 原文地址:https://www.cnblogs.com/renzhituteng/p/11198701.html
Copyright © 2011-2022 走看看