關於在一段字符串中提取所有兩個字符中間的字符串
這里的提取無非也就是自己寫了一個方便自己使用的方法,這個方法里面用到了遞歸和ref,而使用ref的時候必須得先在調用該方法之前初始化一個值,在調用完該方法以后,帶有ref前綴的變量的值就是調用方法完了以后變成的值了!!
/// <summary>
/// 把兩個字符中間的字符提取出來
/// </summary>
/// <param name="str">字符串</param>
/// <param name="a">第一個字符</param>
/// <param name="b">第二個字符</param>
/// <param name="le">第一個字符初始開始數的索引</param>
/// <param name="ri">第二個字符初始開始數的索引</param>
/// <param name="list">提取出來的字符組成的集合</param>
public void getArryList(string str, char a, char b, int le, int ri, ref List<string> list)
{
int left = le;
int right = ri;
int one = str.IndexOf('[', left + 1);
int two = str.IndexOf(']', right + 1);
left = one;
right = two;
if (one >= 0 && two >= 0)
{
list.Add(str.Substring(one + 1, right - (left + 1)));
int i = str.Length;
int qq = str.LastIndexOf('[');
int ii = str.LastIndexOf(']');
if (left != str.LastIndexOf('[') && right != str.LastIndexOf(']'))
{
getArryList(str, a, b, left, right, ref list) ;
}
}
}
下面是如何去使用上面的方法
List<string> list = new List<string>();;
string code="[1]表示教師[2]表示學生";
getArryList(code, '[', ']', 0, 0, ref list);
調用完方法以后字符串的集合就有1和2了