我需要解析一个 url,以便只有我的服务器显示“NA30” 但是,当我进行拆分时,我似乎无法只获取 na30。我试过用'。'修剪和'/',我想我只是弄错了数组部分。有什么指导吗?
链接 https://na30.salesforce.com
我现在在做什么
string thisUrl;
if (Helper.InstanceUrl.Contains(@"://"))
{
thisUrl = Helper.InstanceUrl.Split(new[] { "://" }, 2, StringSplitOptions.None)[1];
return thisUrl.Split('/')[0].Split('.')[0];
}
return "";
最佳答案
您还可以使用 Uri 类找到您的字符串
Uri u = new Uri("https://na30.salesforce.com");
Console.WriteLine(u.Host.Split('.')[0]);
一个值得阅读的问题是 What's the difference between Uri.Host and Uri.Authority
关于c# - 拆分一个字符串,使其丢失 http ://and anything after the '.' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40662741/