亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

刪除字符串開頭和結尾之間的部分

刪除字符串開頭和結尾之間的部分

C#
FFIVE 2023-08-20 10:10:48
首先代碼:   string myString = "<at>onePossibleName</at> some question here regarding <at>disPossibleName</at>"    // some code to handle myString and save it in myEditedString    Console.WriteLine(myEditedString);    //output now is: some question here regarding <at>disPossibleName</at>我想<at>onePossibleName</at>從 myString 中刪除。該字符串onePossibleName可以disPossbileName是任何其他字符串。到目前為止我正在與string myEditedString = string.Join(" ", myString.Split(' ').Skip(1));這里的問題是,如果onePossibleName變成one Possible Name。嘗試也是如此myString.Remove(startIndex, count)- 這不是解決方案。
查看完整描述

3 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

根據你想要的,會有不同的方法,你可以使用 IndexOf 和 SubString,正則表達式也是一個解決方案。


// SubString and IndexOf method

// Usefull if you don't care of the word in the at tag, and you want to remove the first at tag

if (myString.Contains("</at>"))

{

    var myEditedString = myString.Substring(myString.IndexOf("</at>") + 5);

}

// Regex method

var stringToRemove = "onePossibleName";

var rgx = new Regex($"<at>{stringToRemove}</at>");

var myEditedString = rgx.Replace(myString, string.Empty, 1); // The 1 precise that only the first occurrence will be replaced



查看完整回答
反對 回復 2023-08-20
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

string myString = "<at>onePossibleName</at> some question here regarding <at>disPossibleName</at>"


int sFrom = myString.IndexOf("<at>") + "<at>".Length;

int sTo = myString.IndexOf("</at>");


string myEditedString = myString.SubString(sFrom, sFrom - sTo);

Console.WriteLine(myEditedString);

//output now is: some question here regarding <at>disPossibleName</at>


查看完整回答
反對 回復 2023-08-20
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

您可以使用這個通用正則表達式。


var myString = "<at>onePossibleName</at> some question here regarding <at>disPossibleName</at>";

var rg = new Regex(@"<at>(.*?)<\/at>");

var result = rg.Replace(myString, "").Trim();

這將刪除所有“at”標簽及其之間的內容。該Trim()調用是在替換后刪除字符串開頭/結尾處的所有空格。


查看完整回答
反對 回復 2023-08-20
  • 3 回答
  • 0 關注
  • 185 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號