字符串string操作

        #region String
        /// <summary>
        /// 首字母大写
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>首字母大写</returns>
        public static string ToTitleCase(this string str)
        {
            return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str);
        }

        /// <summary>
        /// 对字符串进行URL加密
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string UrlEncode(this string content, string encode = "gb2312")
        {
            return HttpUtility.UrlEncode(content, Encoding.GetEncoding(encode));
        }

        /// <summary>
        /// 对字符串进行URL解密
        /// </summary>
        /// <param name="code">加密字符串</param>
        /// <param name="encode">加密编码格式</param>
        /// <returns></returns>
        public static string UrlDecode(this string content, string encode = "gb2312")
        {
            return HttpUtility.UrlDecode(content, Encoding.GetEncoding(encode));
        }

        /// <summary>
        /// 获取与正则匹配的第一个项值
        /// </summary>
        /// <param name="remark"></param>
        /// <returns></returns>
        public static string RegexMatch(this string content, string Regex)
        {
            MatchCollection matchs = content.RegexMatches(Regex);
            return matchs.Count > 0 ? matchs[0].Value : "";
        }
        /// <summary>
        /// 获取与正则匹配的第一个项值
        /// </summary>
        /// <param name="remark"></param>
        /// <returns></returns>
        public static MatchCollection RegexMatches(this string content, string Regex)
        {
            Regex reg = new Regex(Regex, RegexOptions.IgnoreCase);
            return reg.Matches(content);
        }
        #endregion

 

posted @ 2018-12-28 14:03  木子zzgxl  阅读(136)  评论(0编辑  收藏  举报