如何获得c#中的URL部分?

问题描述:

我有一个像下面的字符串。如何获得c#中的URL部分?

string filePath = @"http://s.ion.com/abc/Std/isd/text.txt" or @"http://s.ion.com/abc/Std/isd/wer/we/wed/text.txt" or @"http://s.ion.com/abc/Std/isd/wer/we/wed/sa/ser/text.txt" 

我需要输出作为@"http://s.ion.com/abc/Std/isd"

我已经试过substring.IndxcOf方法和我单独ISD或性病。

请帮忙。

string filePath = @"http://s.ion.com/abc/Std/isd/text.txt"; 
int index = filePath.LastIndexOf('/'); 
string url = filePath.Substring(0, index); 
+0

如果想,如果我有这样的字符串文件路径= @“http://s.ion.com/abc/Std/isd/ser/wer/text.txt路径“;我怎么能继续得到相同的输出? –

+0

你的意思是,如果在字符串前没有“http://”,你如何在它前面得到一个带有“http://”的输出?如果(!url.StartsWith(@“http://”)){url = @“http://”+ url; } –

+0

你错了。如果字符串是“http://s.ion.com/abc/Std/isd/ser/wer/text”,我需要输出“http://s.ion.com/abc/Std/isd”。文本”; –

原始响应:

字符串转换为Uri对象,你可以做到以下几点:

//filePath = @"http://s.ion.com/abc/Std/isd/text.txt" 

Uri uri = new Uri(filePath); 
string output = uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.Last().Length - 1); // -1 removes the '/' character at the end 

// output = "http://s.ion.com/abc/Std/isd" 

*注:Last()功能是从System.Linq库。如果您不使用此库,则仍可通过用uri.Segments[uri.Segments.Length - 1].Length替换uri.Segments.Last().Length来获取最后一个段。

更新基于this comment响应

//filePath = @"http://s.ion.com/abc/Std/isd/ser/wer/text.txt" 

Uri uri = new Uri(filePath); 
string output = uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.[uri.Segments.Length - 3].Length - 1); 

// uri.Segments.Length - 3 removes the last 3 unrequired "segments" 
// -1 removes the '/' character at the end 

// output = "http://s.ion.com/abc/Std/isd" 

根据最新修订更新的响应

//filePath = @"http://s.ion.com/abc/Std/isd/wer/we/wed/sa/ser/text.txt" 

Uri uri = new Uri(filePath); 
string output = uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.[uri.Segments.Length - 6].Length - 1); 

// uri.Segments.Length - 6 removes the last 6 unrequired "segments" 
// -1 removes the '/' character at the end 

// output = "http://s.ion.com/abc/Std/isd" 

如果这三个字符串是可能的,那么你可以做一个条件语句来确定要操纵的字符串。

if (/* string 1 */) 
    // see original response 
else if (/* string 2 */) 
    // see first updated response 
else if (/* string 3 */) 
    // see second updated response 
+0

如果假设我有一个像string这样的路径filePath = @“s.ion.com/abc/Std/isd/ser/wer /text.txt“;;我怎么能继续得到相同的输出? –

+0

@ user2505309查看我的更新回复。我保留'http://'以保持我答案的完整性。你需要不用'http://'吗? – 2016-04-28 19:43:17

+0

8我需要http://。现在我已经修改了原始查询。请查看 –

我不确定在上下文中,但我总是尝试保留文件名和路径,以便以后修改代码。

string filePath = @"http://s.ion.com/abc/Std/isd"; 
string filename = "text.txt"; 

您应该始终能够使用Path.GetDirectoryName获取路径。如果你需要其他文件名,它会使代码更加清洁。

+0

相同的输出,如果假设如果我有一个像字符串的路径filePath = @“s.ion.com/abc/Std/isd/ser/wer/text.txt”;;我怎么能继续得到相同的输出? –

怎么样的正则表达式

var url = "http://s.ion.com/abc/Std/isd/text.txt"; 
// or  "http://s.ion.com/abc/Std/isd/wer/we/wed/text.txt" 
// or  "http://s.ion.com/abc/Std/isd/wer/we/wed/sa/ser/text.txt" 
// or  "s.ion.com/abc/Std/isd/ser/wer/text.txt" 

var match = Regex.Match(url, @"^(?:http:\/\/)?(.*isd)(?=\/)"); 
Console.WriteLine("http://" + match.Groups[1]); 

// output: http://s.ion.com/abc/Std/isd 
+0

输出将是相同的,无论它已经包含'http://'方案,或您所在的子目录。 –