旧:https://stackoverflow.com/questions/ask
新功能:/questions/ask
我想从第三个“ /”字符开始获取字符串。
答案 0 :(得分:1)
为此,您最好创建一个Uri:
Uri u = new Uri("http://host.com/path/part");
Console.WriteLine(u.PathAndQuery);
不是字符串的所有内容都应作为字符串处理,尤其是当内容具有更深的含义/表示形式(JSON,XML,日期等)时
https://docs.microsoft.com/en-us/dotnet/api/system.uri?view=netframework-4.8
答案 1 :(得分:0)
一个简单但可行的例子。您可以进一步改进它。致电
GetEverythingFromThird("https://stackoverflow.com/questions/ask", "/");
方法:
static string GetEverythingFromThird(string instring, string checkitem)
{
string outstring = "";
//1st occurence
int index = instring.IndexOf(checkitem);
outstring = instring.Substring(index + 1);
//2nd occurence
index = outstring.IndexOf(checkitem);
outstring = outstring.Substring(index + 1);
//3rd occurence
index = outstring.IndexOf(checkitem);
outstring = outstring.Substring(index);
return outstring;
}
答案 2 :(得分:0)
只需使用正则表达式即可。这样的事情应该可以正常工作。
Match res = new Regex(@"(?:.*?\/.*?\/.*?)(?<YourString>\/.*)")
.Match("https://stackoverflow.com/questions/ask");
if (res.Success)
{
return res.Groups["YourString"].Value;
}
如果您仅使用Caius Jard的url解决方案,那就去吧。