增加截取指定内容直接方法

This commit is contained in:
不做码农 2022-01-08 21:46:48 +08:00
parent 175475af62
commit 4cb7296767

View File

@ -106,5 +106,33 @@ namespace Infrastructure.Extensions
return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToUpper() + str[1..];
}
/// <summary>
/// 截取指定字符串中间内容
/// </summary>
/// <param name="sourse"></param>
/// <param name="startstr"></param>
/// <param name="endstr"></param>
/// <returns></returns>
public static string SubstringBetween(this string sourse, string startstr, string endstr)
{
string result = string.Empty;
int startindex, endindex;
try
{
startindex = sourse.IndexOf(startstr);
if (startindex == -1)
return result;
string tmpstr = sourse.Substring(startindex + startstr.Length);
endindex = tmpstr.IndexOf(endstr);
if (endindex == -1)
return result;
result = tmpstr.Remove(endindex);
}
catch (Exception ex)
{
Console.WriteLine("MidStrEx Err:" + ex.Message);
}
return result;
}
}
}