3 回答
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
我認(rèn)為你需要的是string.Remove方法。請(qǐng)參閱此處的 MSDN 文檔:https ://docs.microsoft.com/en-us/dotnet/api/system.string.remove?view=netframework-4.7.2#System_String_Remove_System_Int32_System_Int32_
如果您不知道您的角色在哪里,請(qǐng)先致電string.IndexOf找到它。如果此調(diào)用返回非負(fù)數(shù),則調(diào)用Remove將其刪除。請(qǐng)注意,字符串是不可變的,因此它總是會(huì)創(chuàng)建一個(gè)新對(duì)象。
TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用Remove和IndexOf。
var str = "01123456";
var i = str.IndexOf('1'); // IndexOf returns -1 when there is no element found, so we need to handle that when calling remove.
var res = (i >= 0) ? str.Remove(i, 1) : str;
Console.WriteLine(res); // 0123456
- 3 回答
- 0 關(guān)注
- 202 瀏覽
添加回答
舉報(bào)
