base64编码
/// </summary>
/// <param name="input">字符串</param>
/// <returns>base64编码串</returns>
public static string Base64StringEncode(string input)
{
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(input);
return Convert.ToBase64String(encbuff);
}
/// <summary>
/// 对字符串进行反编码
/// </summary>
/// <param name="input">base64编码串</param>
/// <returns>字符串</returns>
public static string Base64StringDecode(string input)
{
byte[] decbuff = Convert.FromBase64String(input);
return System.Text.Encoding.UTF8.GetString(decbuff);
}
/// <summary>
/// 替换字符串(忽略大小写)
/// </summary>
/// <param name="input">要进行替换的内容</param>
/// <param name="oldValue">旧字符串</param>
/// <param name="newValue">新字符串</param>
/// <returns>替换后的字符串</returns>
public static string CaseInsensitiveReplace(string input, string oldValue, string newValue)
{
Regex regEx = new Regex(oldValue, RegexOptions.IgnoreCase | RegexOptions.Multiline);
return regEx.Replace(input, newValue);
}
/// <summary>
/// 替换首次出现的字符串
/// </summary>
/// <param name="input">要进行替换的内容</param>
/// <param name="oldValue">旧字符串</param>
/// <param name="newValue">新字符串</param>
/// <returns>替换后的字符串</returns>
public static string ReplaceFirst(string input, string oldValue, string newValue)
{
Regex regEx = new Regex(oldValue, RegexOptions.Multiline);
return regEx.Replace(input, newValue, 1);
}
/// <summary>
/// 替换最后一次出现的字符串
/// </summary>
/// <param name="input">要进行替换的内容</param>
/// <param name="oldValue">旧字符串</param>
/// <param name="newValue">新字符串</param>
/// <returns>替换后的字符串</returns>
public static string ReplaceLast(string input, string oldValue, string newValue)
{
int index = input.LastIndexOf(oldValue);
if (index < 0)
{
return input;
}
else
{
StringBuilder sb = new StringBuilder(input.Length - oldValue.Length + newValue.Length);
sb.Append(input.Substring(0, index));
sb.Append(newValue);
sb.Append(input.Substring(index + oldValue.Length, input.Length - index - oldValue.Length));
return sb.ToString();
}
}
/// <summary>
/// 根据词组过虑字符串(忽略大小写)
/// </summary>
/// <param name="input">要进行过虑的内容</param>
/// <param name="filterWords">要过虑的词组</param>
/// <returns>过虑后的字符串</returns>
public static string FilterWords(string input, params string[] filterWords)
{
return StringHelper.FilterWords(input, char.MinValue, filterWords);
}
/// <summary>
/// 根据词组过虑字符串(忽略大小写)
/// </summary>
/// <param name="input">要进行过虑的内容</param>
/// <param name="mask">字符掩码</param>
/// <param name="filterWords">要过虑的词组</param>
/// <returns>过虑后的字符串</returns>
public static string FilterWords(string input, char mask, params string[] filterWords)
{
string stringMask = mask == char.MinValue ? string.Empty : mask.ToString();
string totalMask = stringMask;
foreach (string s in filterWords)
{
Regex regEx = new Regex(s, RegexOptions.IgnoreCase | RegexOptions.Multiline);
if (stringMask.Length > 0)
{
for (int i = 1; i < s.Length; i++)
totalMask += stringMask;
}
input = regEx.Replace(input, totalMask);
totalMask = stringMask;
}
return input;
}
public static MatchCollection HasWords(string input, params string[] hasWords)
{
StringBuilder sb = new StringBuilder(hasWords.Length + 50);
//sb.Append("[");
foreach (string s in hasWords)
{
sb.AppendFormat("({0})|", StringHelper.HtmlSpecialEntitiesEncode(s.Trim()));
}
string pattern = sb.ToString();
pattern = pattern.TrimEnd('|'); // +"]";
Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
return regEx.Matches(input);
}
/// <summary>
/// Html编码
/// </summary>
/// <param name="input">要进行编辑的字符串</param>
/// <returns>Html编码后的字符串</returns>
public static string HtmlSpecialEntitiesEncode(string input)
{
return HttpUtility.HtmlEncode(input);
}
/// <summary>
/// Html解码
/// </summary>
/// <param name="input">要进行解码的字符串</param>
/// <returns>解码后的字符串</returns>
public static string HtmlSpecialEntitiesDecode(string input)
{
return HttpUtility.HtmlDecode(input);
}
/// <summary>
/// MD5加密
/// </summary>
/// <param name="input">要进行加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string MD5String(string input)
{
MD5 md5Hasher = MD5.Create();
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
/// <summary>
/// 对字符串进行MD5较验
/// </summary>
/// <param name="input">要进行较验的字符串</param>
/// <param name="hash">散列串</param>
/// <returns>是否匹配</returns>
public static bool MD5VerifyString(string input, string hash)
{
string hashOfInput = StringHelper.MD5String(input);
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}
public static string PadLeftHtmlSpaces(string input, int totalSpaces)
{
string space = " ";
return PadLeft(input, space, totalSpaces * space.Length);
}
public static string PadLeft(string input, string pad, int totalWidth)
{
return StringHelper.PadLeft(input, pad, totalWidth, false);
}
public static string PadLeft(string input, string pad, int totalWidth, bool cutOff)
{
if (input.Length >= totalWidth)
return input;
int padCount = pad.Length;
string paddedString = input;
while (paddedString.Length < totalWidth)
{
paddedString += pad;
}
// trim the excess.
if (cutOff)
paddedString = paddedString.Substring(0, totalWidth);
return paddedString;
}
public static string PadRightHtmlSpaces(string input, int totalSpaces)
{
string space = " ";
return PadRight(input, space, totalSpaces * space.Length);
}
public static string PadRight(string input, string pad, int totalWidth)
{
return StringHelper.PadRight(input, pad, totalWidth, false);
}
public static string PadRight(string input, string pad, int totalWidth, bool cutOff)
{
if (input.Length >= totalWidth)
return input;
string paddedString = string.Empty;
while (paddedString.Length < totalWidth - input.Length)
{
paddedString += pad;
}
// trim the excess.
if (cutOff)
paddedString = paddedString.Substring(0, totalWidth - input.Length);
paddedString += input;
return paddedString;
}
/// <summary>
/// 去除新行
/// </summary>
/// <param name="input">要去除新行的字符串</param>
/// <returns>已经去除新行的字符串</returns>
public static string RemoveNewLines(string input)
{
return StringHelper.RemoveNewLines(input, false);
}
/// <summary>
/// 去除新行
/// </summary>
/// <param name="input">要去除新行的字符串</param>
/// <param name="addSpace">是否添加空格</param>
/// <returns>已经去除新行的字符串</returns>
public static string RemoveNewLines(string input, bool addSpace)
{
string replace = string.Empty;
if (addSpace)
replace = " ";
string pattern = @"[/r|/n]";
Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
return regEx.Replace(input, replace);
}
/// <summary>
/// 字符串反转
/// </summary>
/// <param name="input">要进行反转的字符串</param>
/// <returns>反转后的字符串</returns>
public static string Reverse(string input)
{
char[] reverse = new char[input.Length];
for (int i = 0, k = input.Length - 1; i < input.Length; i++, k--)
{
if (char.IsSurrogate(input[k]))
{
reverse[i + 1] = input[k--];
reverse[i++] = input[k];
}
else
{
reverse[i] = input[k];
}
}
return new System.String(reverse);
}
/// <summary>
/// 转成首字母大字形式
/// </summary>
/// <param name="input">要进行转换的字符串</param>
/// <returns>转换后的字符串</returns>
public static string SentenceCase(string input)
{
if (input.Length < 1)
return input;
string sentence = input.ToLower();
return sentence[0].ToString().ToUpper() + sentence.Substring(1);
}
/// <summary>
/// 空格转换成
/// </summary>
/// <param name="input">要进行转换的字符串</param>
/// <returns>转换后的字符串</returns>
public static string SpaceToNbsp(string input)
{
string space = " ";
return input.Replace(" ", space);
}
/// <summary>
/// 去除"<" 和 ">" 符号之间的内容
/// </summary>
/// <param name="input">要进行处理的字符串</param>
/// <returns>处理后的字符串</returns>
public static string StripTags(string input)
{
Regex stripTags = new Regex("<(.|/n)+?>");
return stripTags.Replace(input, "");
}
public static string TitleCase(string input)
{
return TitleCase(input, true);
}
public static string TitleCase(string input, bool ignoreShortWords)
{
List<string> ignoreWords = null;
if (ignoreShortWords)
{
//TODO: Add more ignore words?
ignoreWords = new List<string>();
ignoreWords.Add("a");
ignoreWords.Add("is");
ignoreWords.Add("was");
ignoreWords.Add("the");
}
string[] tokens = input.Split(' ');
StringBuilder sb = new StringBuilder(input.Length);
foreach (string s in tokens)
{
if (ignoreShortWords == true
&& s != tokens[0]
&& ignoreWords.Contains(s.ToLower()))
{
sb.Append(s + " ");
}
else
{
sb.Append(s[0].ToString().ToUpper());
sb.Append(s.Substring(1).ToLower());
sb.Append(" ");
}
}
return sb.ToString().Trim();
}
/// <summary>
/// 去除字符串内的空白字符
/// </summary>
/// <param name="input">要进行处理的字符串</param>
/// <returns>处理后的字符串</returns>
public static string TrimIntraWords(string input)
{
Regex regEx = new Regex(@"[/s]+");
return regEx.Replace(input, " ");
}
/// <summary>
/// 换行符转换成Html标签的换行符<br />
/// </summary>
/// <param name="input">要进行处理的字符串</param>
/// <returns>处理后的字符串</returns>
public static string NewLineToBreak(string input)
{
Regex regEx = new Regex(@"[/n|/r]+");
return regEx.Replace(input, "<br />");
}
/// <summary>
/// 插入换行符(不中断单词)
/// </summary>
/// <param name="input">要进行处理的字符串</param>
/// <param name="charCount">每行字符数</param>
/// <returns>处理后的字符串</returns>
public static string WordWrap(string input, int charCount)
{
return StringHelper.WordWrap(input, charCount, false, Environment.NewLine);
}
/// <summary>
/// 插入换行符
/// </summary>
/// <param name="input">要进行处理的字符串</param>
/// <param name="charCount">每行字符数</param>
/// <param name="cutOff">如果为真,将在单词的中部断开</param>
/// <returns>处理后的字符串</returns>
public static string WordWrap(string input, int charCount, bool cutOff)
{
return StringHelper.WordWrap(input, charCount, cutOff, Environment.NewLine);
}
/// <summary>
/// 插入换行符
/// </summary>
/// <param name="input">要进行处理的字符串</param>
/// <param name="charCount">每行字符数</param>
/// <param name="cutOff">如果为真,将在单词的中部断开</param>
/// <param name="breakText">插入的换行符号</param>
/// <returns>处理后的字符串</returns>
public static string WordWrap(string input, int charCount, bool cutOff, string breakText)
{
StringBuilder sb = new StringBuilder(input.Length + 100);
int counter = 0;
if (cutOff)
{
while (counter < input.Length)
{
if (input.Length > counter + charCount)
{
sb.Append(input.Substring(counter, charCount));
sb.Append(breakText);
}
else
{
sb.Append(input.Substring(counter));
}
counter += charCount;
}
}
else
{
string[] strings = input.Split(' ');
for (int i = 0; i < strings.Length; i++)
{
counter += strings[i].Length + 1; // the added one is to represent the inclusion of the space.
if (i != 0 && counter > charCount)
{
sb.Append(breakText);
counter = 0;
}
sb.Append(strings[i] + ' ');
}
}
return sb.ToString().TrimEnd(); // to get rid of the extra space at the end.
}
#endregion
}
}