C# 在最后一个斜杠后将值插入字符串

C# 在最后一个斜杠后将值插入字符串,c#,regex,C#,Regex,我需要在最后一个斜杠后插入一些字符串值。我有这样的字符串值: string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug"; 我需要得到这个值: "http://blog.loc/blog/news/sport/hot_slug1_slug2_slug3-slug" 因此,我需要在最后一个斜杠后插入hot\uuu(例如)。有人能帮我吗?我知道你要的是regex,但我认为这不是真的必要 您只需使用: url现在保存值

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

string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
我需要得到这个值:

"http://blog.loc/blog/news/sport/hot_slug1_slug2_slug3-slug"

因此,我需要在最后一个斜杠后插入
hot\uuu
(例如)。有人能帮我吗?

我知道你要的是regex,但我认为这不是真的必要

您只需使用:

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

正则表达式方法:

string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
var matches = Regex.Matches(url, "/");
var match = matches[matches.Count - 1];
string result = url.Insert(match.Index + 1, "hot_")

您已经标记了这个正则表达式,您是否希望答案也是正则表达式(因为这不一定需要)?我只需要任何好的解决方案)以秒为单位击败我……)虽然正确,但我不喜欢LastIndexOf在测试字符串中返回正确位置的假设LastIndexOf是获取正确索引的一个简单示例。我相信OP可以找到自己的方法,如果他发现它不可靠的话。一些服务器解释以
/
结尾的URL的方式与解释没有
/
的URL的方式相同。出于这个原因,这个答案并不像imo所能给出的那么可靠
string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
var matches = Regex.Matches(url, "/");
var match = matches[matches.Count - 1];
string result = url.Insert(match.Index + 1, "hot_")