2016-04-28 71 views
1

我有一個像下面的字符串。如何獲得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或性病。

請幫忙。

回答

0
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」。文本」; –

2

原始響應:

字符串轉換爲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://。現在我已經修改了原始查詢。請查看 –

-1

我不確定在上下文中,但我總是嘗試保留文件名和路徑,以便以後修改代碼。

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」;;我怎麼能繼續得到相同的輸出? –

0

怎麼樣的正則表達式

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://'方案,或您所在的子目錄。 –