1.返回结果为列表<满足条件的字符串>
.
/// <summary>
/// 截取两个指定字符串中间的字符串。
/// </summary>
/// <param name="outs">输出,返回列表</param>
/// <param name="content">待处理的字符串</param>
/// <param name="startStr">判断字符串首</param>
/// <param name="endStr">判断字符串尾</param>
/// <returns></returns>
private bool GetAllSubstring_RList(out List<string>? outs, string content, string startStr, string endStr)
{
if (startStr == "" || endStr == "") { outs = null; return false; }
int startLen = startStr.Length;//判断字符串首的长度
int endLen = endStr.Length;//判断字符串尾的长度
List<string> outList = new List<string>();//待返回的结果列表
int BNo = -1, ENo = -1, Len, SubB;
string ReremainderStr = content;//余下的字符串
string SubStr;
while (true)
{
Len = ReremainderStr.Length;//余下的待处理的字符串的长度
BNo = ReremainderStr.IndexOf(startStr);
if (BNo > -1) //寻找到了起始字符串
{
SubB = BNo + startLen;
//截取满足条件之后的全部剩余字符串作为剩余待处理字符串
ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
Len = ReremainderStr.Length;//更新长度
ENo = ReremainderStr.IndexOf(endStr);//寻找结尾字符串
if (ENo > -1)//寻找到了结束字符串
{
SubStr = ReremainderStr.Substring(0, ENo);//截取中间字符串
outList.Add(SubStr);//添加进入结果列表
SubB = ENo + endLen;
//截取满足条件之后的全部剩余字符串作为剩余待处理字符串
ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
}
else break;
}
else break;
}
if (outList.Count == 0) { outs = null; return false; }
outs = outList;
return true;
}
2.返回结果为字典<满足条件的字符串在输入字符串的首个位置,满足条件的字符串>
.
/// <summary>
/// 截取两个指定字符串中间的字符串。
/// </summary>
/// <param name="outs">输出,返回字典</param>
/// <param name="content">待处理的字符串</param>
/// <param name="startStr">判断字符串首</param>
/// <param name="endStr">判断字符串尾</param>
/// <returns></returns>
private bool GetAllSubstring_RDic(out Dictionary<int, string>? outs,string content, string startStr, string endStr)
{
if (startStr == "" || endStr == "") { outs = null;return false; }
int startLen = startStr.Length;
int endLen = endStr.Length;
Dictionary<int,string> outList = new Dictionary<int, string>();
int BNo = -1, ENo = -1, Len, SubB;
int HeadLen = 0;//已舍弃的头部长度
string ReremainderStr = content;
string SubStr;
while (true)
{
Len = ReremainderStr.Length;
BNo = ReremainderStr.IndexOf(startStr);
if (BNo > -1) //寻找到了起始字符串
{
SubB = BNo + startLen;
HeadLen += SubB;
//截取满足条件之后的全部剩余字符串作为剩余待处理字符串
ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
Len = ReremainderStr.Length;//更新长度
ENo = ReremainderStr.IndexOf(endStr);//寻找结尾字符串
if (ENo > -1)//寻找到了结束字符串
{
SubStr = ReremainderStr.Substring(0, ENo);//截取中间字符串
outList.TryAdd(HeadLen, SubStr);//添加进入结果列表
SubB = ENo + endLen;
HeadLen += SubB;
//截取满足条件之后的全部剩余字符串作为剩余待处理字符串
ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
}
else break;
}
else break;
}
if (outList.Count == 0) { outs = null; return false; }
outs = outList;
return true;
}
3.返回结果为字典<满足条件的字符串,该字符串出现的次数>
.
/// <summary>
/// 截取两个指定字符串中间的字符串。
/// </summary>
/// <param name="outs">输出,返回字典</param>
/// <param name="content">待处理的字符串</param>
/// <param name="startStr">判断字符串首</param>
/// <param name="endStr">判断字符串尾</param>
/// <returns></returns>
private bool GetAllSubstring_RDic(out Dictionary<string, int>? outs, string content, string startStr, string endStr)
{
if (startStr == "" || endStr == "") { outs = null; return false; }
int startLen = startStr.Length;
int endLen = endStr.Length;
Dictionary<string, int> outList = new Dictionary<string, int>();
int BNo = -1, ENo = -1, Len, SubB;
string ReremainderStr = content;
string SubStr;
while (true)
{
Len = ReremainderStr.Length;
BNo = ReremainderStr.IndexOf(startStr);
if (BNo > -1) //寻找到了起始字符串
{
SubB = BNo + startLen;
//截取满足条件之后的全部剩余字符串作为剩余待处理字符串
ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
Len = ReremainderStr.Length;//更新长度
ENo = ReremainderStr.IndexOf(endStr);//寻找结尾字符串
if (ENo > -1)//寻找到了结束字符串
{
SubStr = ReremainderStr.Substring(0, ENo);//截取中间字符串
if(outList.ContainsKey(SubStr))
{
outList[SubStr] += 1;
}
else outList.TryAdd(SubStr, 1);//添加进入结果列表
SubB = ENo + endLen;
//截取满足条件之后的全部剩余字符串作为剩余待处理字符串
ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
}
else break;
}
else break;
}
if (outList.Count == 0) { outs = null; return false; }
outs = outList;
return true;
}