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

时间:2015-12-18 10:35:05

标签: 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_(例如)。有谁可以帮助我?

2 个答案:

答案 0 :(得分:13)

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

您可以使用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

答案 1 :(得分:2)

正则表达式方法:

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_")
相关问题