You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

107 lines
2.3 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace System
{
public static class StringExtensions
{
/// <summary>
/// 将字符串作为新List的第一个元素
/// </summary>
public static IList<string> AsFirstValueOfList(this string source)
{
var list = new List<string>();
list.Add(source);
return list;
}
public static bool IsNullOrEmpty(this string s)
{
return string.IsNullOrEmpty(s);
}
#region FormatWith:string.Format。后面三个方法,是为了提高效率而加的:比第一种方法效率高
public static string FormatWith(this string format, params object[] args)
{
return string.Format(format, args);
}
public static string FormatWith(this string format, object arg0)
{
return string.Format(format, arg0);
}
public static string FormatWith(this string format, object arg0, object arg1)
{
return string.Format(format, arg0, arg1);
}
public static string FormatWith(this string format, object arg0, object arg1, object arg2)
{
return string.Format(format, arg0, arg1, arg2);
}
#endregion
public static bool IsMatch(this string s, string pattern)
{
if (s == null)
return false;
else
return Regex.IsMatch(s, pattern);
}
public static string Match(this string s, string pattern)
{
if (s == null)
return "";
return Regex.Match(s, pattern).Value;
}
public static bool IsInt(this string s)
{
int i;
return int.TryParse(s, out i);
}
public static int ToInt(this string s)
{
return int.Parse(s);
}
/// <summary>
/// 首字母转换成小写
/// </summary>
public static string ToCamel(this string s)
{
if (s.IsNullOrEmpty())
return s;
return s[0].ToString().ToLower() + s.Substring(1);
}
/// <summary>
/// 首字母转换成大写
/// </summary>
public static string ToPascal(this string s)
{
if (s.IsNullOrEmpty())
return s;
return s[0].ToString().ToUpper() + s.Substring(1);
}
public static string ToUnicodeString(this byte[] arr)
{
return System.Text.Encoding.Unicode.GetString(arr);
}
public static byte[] EncodeUnicodePwd(this string password)
{
using (var md5 = MD5.Create())
return md5.ComputeHash(Encoding.Unicode.GetBytes(password));
}
}
}