問題
在unity開發(fā)過程中糯耍,如果一個string字符串有多行庭猩,如果我們想刪除前面一行或者多行應(yīng)該如何操作狡孔?
解決方案
private static string DeleteStrLine(string text, int startLine, int lineCount)
{
var curIndex = 0;
int? remStartIndex = null;
var sum = 1;
while (sum < startLine + lineCount)
{
if (sum == startLine) remStartIndex = curIndex;
curIndex = text.IndexOf("\n", curIndex, StringComparison.Ordinal);
if (curIndex < 0)
{
curIndex = text.Length;
break;
}
curIndex++;
sum++;
}
if (remStartIndex == null)
{
return text;
}
text = text.Remove(remStartIndex.Value, curIndex - remStartIndex.Value);
return text;
}
思路就是通過IndexOf函數(shù)遍歷找到需要刪除的行對應(yīng)的"\n"(換行)的索引秫舌,然后再通過Remove函數(shù)對開始和結(jié)束的索引進(jìn)行刪除氯材。