我需要在最后一个斜杠后插入一些字符串值。我的字符串值如下:
string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
我需要获取这个值:
"http://blog.loc/blog/news/sport/hot_slug1_slug2_slug3-slug"
所以,我需要在最后一个斜杠后面插入
hot_
(例如),有人能帮忙吗?我需要在最后一个斜杠后插入一些字符串值。我的字符串值如下:
string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
我需要获取这个值:
"http://blog.loc/blog/news/sport/hot_slug1_slug2_slug3-slug"
hot_
(例如),有人能帮忙吗?我知道你要求使用正则表达式,但在我看来并不是必需的。
你可以直接使用string.Insert:
string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
url = url.Insert(url.LastIndexOf("/") + 1, "hot_");
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_")