Answered by:
Remove last string after special character

Question
-
User-73514677 posted
Hi,
I have a string where in the data is in the format of http://server/sitename/folder1/folder2/123_0.0 . I am using C#.
I would like to remove the last string and get the result in the format of "http://server/sitename/folder1/folder2/".
I have tried using substring and index as below, but the expected output is not shown. How to fix this? Thanks
posturl = posturl.Substring(posturl.IndexOf("\\") + 1 );
Tuesday, November 21, 2017 6:03 AM
Answers
-
User991499041 posted
Hi Venkatzeus,
Hi,
I have a string where in the data is in the format of http://server/sitename/folder1/folder2/123_0.0 . I am using C#.
I would like to remove the last string and get the result in the format of "http://server/sitename/folder1/folder2/".
I have tried using substring and index as below, but the expected output is not shown. How to fix this? Thanks
posturl = posturl.Substring(posturl.IndexOf("\\") + 1 );
Using the Uri class to resolve Uri, you can use the Segment property to access all segments and rebuild the Uri without the last segment.
var myString = "http://server/sitename/folder1/folder2/123_0.0"; var uri = new Uri(myString); var noLastSegment = string.Format("{0}://{1}", uri.Scheme, uri.Authority); for (int i = 0; i < uri.Segments.Length - 1; i++) { noLastSegment += uri.Segments[i]; } noLastSegment = noLastSegment.Trim("/".ToCharArray()); // remove trailing `/` Console.WriteLine(noLastSegment);
Regards,
zxj
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 21, 2017 8:02 AM -
User-1838255255 posted
Hi venkatzeus,
Through your needs, please check the following sample code:
string url = "http://server/sitename/folder1/folder2/123_0.0"; // get index of "/" var index = url.LastIndexOf("/"); // cut the respond string string afterstring = url.Substring(0, index + 1); Response.Write(afterstring);
Result:
Best Regards,
Eric Du
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 21, 2017 8:37 AM
All replies
-
User991499041 posted
Hi Venkatzeus,
Hi,
I have a string where in the data is in the format of http://server/sitename/folder1/folder2/123_0.0 . I am using C#.
I would like to remove the last string and get the result in the format of "http://server/sitename/folder1/folder2/".
I have tried using substring and index as below, but the expected output is not shown. How to fix this? Thanks
posturl = posturl.Substring(posturl.IndexOf("\\") + 1 );
Using the Uri class to resolve Uri, you can use the Segment property to access all segments and rebuild the Uri without the last segment.
var myString = "http://server/sitename/folder1/folder2/123_0.0"; var uri = new Uri(myString); var noLastSegment = string.Format("{0}://{1}", uri.Scheme, uri.Authority); for (int i = 0; i < uri.Segments.Length - 1; i++) { noLastSegment += uri.Segments[i]; } noLastSegment = noLastSegment.Trim("/".ToCharArray()); // remove trailing `/` Console.WriteLine(noLastSegment);
Regards,
zxj
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 21, 2017 8:02 AM -
User-1838255255 posted
Hi venkatzeus,
Through your needs, please check the following sample code:
string url = "http://server/sitename/folder1/folder2/123_0.0"; // get index of "/" var index = url.LastIndexOf("/"); // cut the respond string string afterstring = url.Substring(0, index + 1); Response.Write(afterstring);
Result:
Best Regards,
Eric Du
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 21, 2017 8:37 AM