1

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.

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

if suppose if i have a path like string filePath = @"s.ion.com/abc/Std/isd/ser/wer/text.txt";; how i can proceed to get the same output?
@user2505309 See my updated response. I kept the http:// in to maintain the integrity of my answer. Do you require not having http://?
@user2505309 I have answered for each of your three strings. Please view that.
please check now. all the strings are possible in my case. whatever may be string. but i need to get the output same as i mentioned above
0
string filePath = @"http://s.ion.com/abc/Std/isd/text.txt";
int index = filePath.LastIndexOf('/');
string url = filePath.Substring(0, index);

4 Comments

if suppose if i have a path like string filePath = @"s.ion.com/abc/Std/isd/ser/wer/text.txt"; how i can proceed to get the same output?
do you mean, if there's no "http://" in front of the string how do you get an output with "http://" in front of it? if (!url.StartsWith(@"http://")) { url = @"http://" + url; }
you are wrong. i need to get ouput like "s.ion.com/abc/Std/isd" if the string is "s.ion.com/abc/Std/isd/ser/wer/text.txt";
I think the question is a little unclear as to how you want the uri segments to be divided. If you are receiving /abc/std/isd/ser/wer (or some other combination), how are you deciding which way they should be divided? Also, given that it's not as simple as removing the last segment, @mastermind_ed's solution will be more flexible
0

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

1 Comment

The output will be the same regardless of it already containing the http:// scheme, or which subdirectory you are in.
-1

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.

1 Comment

if suppose if i have a path like string filePath = @"s.ion.com/abc/Std/isd/ser/wer/text.txt";; how i can proceed to get the same output?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.