public static String Substring(this String str, String after, String before = null, Int32 startIndex = 0, Int32[] positions = null)
{
if (String.IsNullOrEmpty(str)) return str;
if (String.IsNullOrEmpty(after) && String.IsNullOrEmpty(before)) return str;
/*
* 1,只有start,从该字符串之后部分
* 2,只有end,从开头到该字符串之前
* 3,同时start和end,取中间部分
*/
var p = -1;
if (!String.IsNullOrEmpty(after))
{
p = str.IndexOf(after, startIndex);
if (p < 0) return null;
p += after.Length;
// 记录位置
if (positions != null && positions.Length > 0) positions[0] = p;
}
if (String.IsNullOrEmpty(before)) return str.Substring(p);
var f = str.IndexOf(before, p >= 0 ? p : startIndex);
if (f < 0) return null;
// 记录位置
if (positions != null && positions.Length > 1) positions[1] = f;
if (p >= 0)
return str.Substring(p, f - p);
else
return str.Substring(0, f);
}