C# 扩展方法简介
public static bool IsNullOrEmpty(this string input)
{
return string.IsNullOrEmpty(input);
}
与此相反,还可以写成
public static bool IsNotNullOrEmpty(this string input)
{
return !string.IsNullOrEmpty(input);
}
即使定义一个string变量,值为null,这两个方法也是可以正常使用的,使用起来比调用string.IsNullOrEmpty(str)会稍微方便些。
public static int ToInt(this string input)
{
return int.Parse(input);
}
public static bool IsInt(this string input)
{
int i;
return int.TryParse(input, out i);
}
同上,可以实现float,double等其他类型的转换和判断,就不一一例举了。
public static string Format(this string format, params object[] args)
{
return string.Format(format, args);
}
public static string Format(this string format, object arg0)
{
return string.Format(format, arg0);
}
public static string Format(this string format, object arg0, object arg1)
{
return string.Format(format, arg0, arg1);
}
public static string Format(this string format, object arg0, object arg1, object arg2)
{
return string.Format(format, arg0, arg1, arg2);
}
public static string Format(this string format, IFormatProvider provider, params object[] args)
{
return string.Format(provider, format, args);
}
public static string Reverse(this string str)
{
var sb = new StringBuilder();
lock (StringBuilder)
{
sb.Remove(0, StringBuilder.Length);
or (var i = str.Length - 1; i >= 0; i--)
sb.Append(str[i]);
return sb.ToString();
}
}
public static string Encode(this string str, Encoding oldEncode, Encoding newEncode)
{
var oldBytes = oldEncode.GetBytes(str);
var newBytes = Encoding.Convert(oldEncode, newEncode, oldBytes);
var result = newEncode.GetString(newBytes);
return result;
}
public static bool IsMatch(this string input, string pattern)
{
return input != null && Regex.IsMatch(input, pattern);
}
public static bool IsMatch(this string input, Regex regex)
{
return input != null && regex.IsMatch(input);
}
public static string Match(this string input, string pattern)
{
return input == null ? "" : Regex.Match(input, pattern).Value;
}
public static string Match(this string input, Regex regex)
{
return input == null ? "" : regex.Match(input).Value;
}
public static int GetLines(this string str) {
if (string.IsNullOrEmpty(str)) return 0;
var splits = str.Split('\r');
return splits.Length;
}
public static string SubStringWithLine(this string str, int startLine, int lines, string endStr = "\n")
{
if (string.IsNullOrEmpty(str))
{
throw new NullReferenceException();
}
var splits = str.Split(new[] { endStr }, StringSplitOptions.None);
var lineCount = splits.Length;
if (startLine < 0 || lines + startLine > lineCount)
{
throw new IndexOutOfRangeException(
"startLine = " + startLine +
",\tlines = " + lines +
",\t Line Count : " + lineCount + ".");
}
var startIndex = 0;
var length = 0;
var index = 0;
for (; index < startLine; index++)
{
startIndex += splits[index].Length + endStr.Length;
}
for (; index < startLine + lines; index++)
{
length += splits[index].Length + endStr.Length;
}
length--;
return str.Substring(startIndex, length);
}
public static void Reverse(this StringBuilder builder)
{
var result = new StringBuilder();
for (var i = builder.Length - 1; i >= 0; i--)
{
result.Append(builder[i]);
}
builder.Remove(0, builder.Length);
builder.Append(result);
}
public static void Clear(this StringBuilder builder)
{
builder.Remove(0, builder.Length);
}