123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Linq.Expressions;
- namespace Bowin.Common.Utility
- {
- public static class StringEx
- {
- /// <summary>
- /// 从左边起截取指定长度的字符串
- /// </summary>
- /// <param name="input"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- public static string Left(this string input, int length)
- {
- if (input.IsEmpty())
- {
- return string.Empty;
- }
- if (input.Length > length)
- {
- return input.Substring(0, length);
- }
- return input;
- }
- /// <summary>
- /// 从左边起截取指定长度的字符串,并追加指定的字符串
- /// </summary>
- /// <param name="input"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- public static string Left(this string input, int length, string plus)
- {
- if (input.IsEmpty())
- {
- return string.Empty;
- }
- if (input.Length <= length)
- {
- return input;
- }
- return string.Format("{0}{1}", Left(input, length - 1), plus);
- }
- /// <summary>
- /// 验证字符串是否为null 空或者由空白字符串组成
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static bool IsEmpty(this string input)
- {
- if (input == null || input.Trim().Length == 0)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 验证字符串不为null 空或者由空白字符串组成
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static bool IsNotEmpty(this string input)
- {
- return !input.IsEmpty();
- }
- public static string MD5(this string input)
- {
- return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "md5");
- }
- public static string ToCamel(this string input)
- {
- if (input.Length == 0)
- {
- return "";
- }
- else
- {
- string firstCode = input.Substring(0, 1).ToLower();
- return firstCode + input.Substring(1, input.Length - 1);
- }
- }
- public static string TrimStart(this string input, string partern)
- {
- if (input.StartsWith(partern))
- {
- return input.Substring(partern.Length);
- }
- else
- {
- return input;
- }
- }
- public static string TrimEnd(this string input, string partern)
- {
- if (input.EndsWith(partern))
- {
- return input.Remove(input.LastIndexOf(partern));
- }
- else
- {
- return input;
- }
- }
- public static string NoHTML(this string Htmlstring)
- {
- //删除脚本
- Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
- //删除HTML
- Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
- //替换单引号 避免js脚本错误 最新文章列表摘要就出错过 duanqh 2011-09-22
- Htmlstring = Htmlstring.Replace("'", "´");
- Htmlstring = Htmlstring.Replace("<", "<");
- Htmlstring = Htmlstring.Replace(">", ">");
- Htmlstring = Htmlstring.Replace("\r\n", "");
- Htmlstring = Htmlstring.Trim();
- return Htmlstring;
- }
- public static string RealTrim(this string source)
- {
- string result = source;
- result = result.Replace("\0", "");
- result = result.Trim();
- return result;
- }
- public static List<Guid?> SplitIDString(this string source, char seperator = ',')
- {
- return source.Split(seperator).Where(x => x != "").Select(x => (Guid?)new Guid(x)).ToList();
- }
- }
- }
|