Ursprüngliche Antwort:
den String in ein Uri
Objekt konvertieren, können Sie folgendes tun:
//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"
* Hinweis: Die Last()
Funktion ist von der System.Linq
Bibliothek. Wenn Sie diese Bibliothek nicht verwenden, können Sie immer noch das letzte Segment erhalten, indem Sie uri.Segments.Last().Length
durch uri.Segments[uri.Segments.Length - 1].Length
ersetzen.
Updated Antwort basierend auf 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"
Aktualisiert Antwort basierend auf der letzten Revision:
//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"
Wenn diese drei Saiten möglich sind, dann können Sie tun eine bedingte Anweisung, um festzustellen, welche Zeichenfolge manipuliert werden soll.
if (/* string 1 */)
// see original response
else if (/* string 2 */)
// see first updated response
else if (/* string 3 */)
// see second updated response
wenn nehme an, wenn ich einen Weg wie Zeichenfolge haben filePath = @ "http://s.ion.com/abc/Std/isd/ser/wer/text.txt "; Wie kann ich fortfahren, um die gleiche Ausgabe zu erhalten? –
meinst du, wenn es kein "http: //" vor der Zeichenkette gibt, wie bekommst du eine Ausgabe mit "http: //" davor? if (! url.StartsWith (@ "http: //")) {url = @ "http: //" + URL; } –
Sie liegen falsch. Ich muss output wie "http://s.ion.com/abc/Std/isd", wenn die Zeichenfolge "http://s.ion.com/abc/Std/isd/ser/wer/text. txt"; –