1
Answer

How to Replace Last three slash from the url with comma(,)

Mohd Arif

Mohd Arif

7y
225
1
I have a url
 
string url="http://www.xyz.com/forums/AddPost/NewPost/Hello";
 
I want to replace last three slash(/) from the url with comma(,).
 
I want url like this 
 
http://www.xyz.com/forums,AddPost,NewPost,Hello 
 
Please Solve my problam. 
Answers (1)
1
Ajeesh

Ajeesh

NA 349 1.6k 7y
You can try with below code
  1. //Your input url  
  2.         Uri inputurl = new Uri("http://www.xyz.com/forums/AddPost/NewPost/Hello");  
  3.           
  4.           
  5.         //Get the domain part of url  
  6.         string requested = inputurl.Scheme + Uri.SchemeDelimiter + inputurl.Host;  
  7.         //Create a URI from it  
  8.         System.UriBuilder uriBuilder = new System.UriBuilder(requested);  
  9.           
  10.         //Replace the lAST THREE slash with comma  
  11.         uriBuilder.Path += inputurl.PathAndQuery.Replace("/"",").TrimStart(',');  
  12.         //ouput the result  
  13.         Uri url = uriBuilder.Uri;  
 Sample Demo of above code can be found here : https://dotnetfiddle.net/IegCnz
Accepted