Reputation: 901
I have a string like below.
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"
I need to get the output as @"http://s.ion.com/abc/Std/isd"
I have tried substring.IndxcOf method and i got isd or std alone.
Please help on this.
Upvotes: 1
Views: 68
Reputation: 1640
How about a regex
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
Upvotes: 0
Reputation:
Original Response:
Converting the string to a Uri
object, you can do the following:
//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"
*Note: the Last()
function is from the System.Linq
library. If you are not using this library, you can still obtain the last segment by replacing uri.Segments.Last().Length
with uri.Segments[uri.Segments.Length - 1].Length
.
Updated Response based on 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"
Updated Response based on the last 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"
If those three strings are possible, then you can do a conditional statement to ascertain which string to manipulate.
if (/* string 1 */)
// see original response
else if (/* string 2 */)
// see first updated response
else if (/* string 3 */)
// see second updated response
Upvotes: 2
Reputation: 57
I am not sure on the context but I always try and keep the file name and path separate for later code modification.
string filePath = @"http://s.ion.com/abc/Std/isd";
string filename = "text.txt";
You should always be able to get your path with a Path.GetDirectoryName. If you need other file names it makes the code much cleaner.
Upvotes: -1
Reputation: 612
string filePath = @"http://s.ion.com/abc/Std/isd/text.txt";
int index = filePath.LastIndexOf('/');
string url = filePath.Substring(0, index);
Upvotes: 0