在最后一个斜杠后将值插入String

Ale*_*kiy 3 c# regex

我需要在最后一个斜杠后插入一些字符串值.我有这样的字符串值:

string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
Run Code Online (Sandbox Code Playgroud)

我需要得到这个值:

"http://blog.loc/blog/news/sport/hot_slug1_slug2_slug3-slug"
Run Code Online (Sandbox Code Playgroud)

所以,我需要hot_在最后一个斜杠之后插入(例如).谁能帮助我?

Equ*_*lsk 13

我知道你要求正则表达式,但在我看来并不是真的有必要.

你可以使用string.Insert:

string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";

url = url.Insert(url.LastIndexOf("/") + 1, "hot_");
Run Code Online (Sandbox Code Playgroud)

url现在保存值: http://blog.loc/blog/news/sport/hot_slug1_slug2_slug3-slug

  • 虽然正确但我不喜欢LastIndexOf在测试字符串中返回正确位置的假设 (3认同)