我有以下內容:string test = "200| ?ρχισε ξαν?| Start again| test | test? |201,202,203,204 |list | picture]"我如何獲得第 5 個“|”右側的所有字符 所以201,202,203,204 |列表 | 圖片。字符數總是不同的。這如何用 c# 來完成?我正在嘗試與line.Substring(line.LastIndexOf('|') -3)但結果是“|圖片”。我也嘗試 line.Split["|"] 但這也不起作用,因為我只想要第 5 個“|”之后的所有內容。
2 回答

米琪卡哇伊
TA貢獻1998條經驗 獲得超6個贊
您可以使用Split
字符分割字符串'|'
,然后使用System.Linq
擴展方法Skip
跳過第一個5
結果,最后string.Join
使用字符將剩余部分再次連接在一起'|'
:
string result = string.Join("|", test.Split('|').Skip(5));

眼眸繁星
TA貢獻1873條經驗 獲得超9個贊
你可以使用這個:
string test = "200| ?ρχισε ξαν?| Start again| test | test? |201,202,203,204 |list | picture]";
int pos = -1;
for ( int i = 1; i <= 5; i++)
{
pos = test.IndexOf('|', pos + 1);
if ( pos == -1 )
break;
}
string result = "";
if ( pos != -1 )
result = test.Substring(pos + 1).Trim();
Console.WriteLine(result);
Console.ReadKey();
或者使用@RufusL linq 版本。
- 2 回答
- 0 關注
- 176 瀏覽
添加回答
舉報
0/150
提交
取消