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
{
///
/// 从左边起截取指定长度的字符串
///
///
///
///
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;
}
///
/// 从左边起截取指定长度的字符串,并追加指定的字符串
///
///
///
///
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);
}
///
/// 验证字符串是否为null 空或者由空白字符串组成
///
///
///
public static bool IsEmpty(this string input)
{
if (input == null || input.Trim().Length == 0)
{
return true;
}
return false;
}
///
/// 验证字符串不为null 空或者由空白字符串组成
///
///
///
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, @"", "", 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, @"