Reputation: 883
I have the string string test="http://www.test.com//web?testid=12"
.
I need to replace with in the string // into /.
Problem is if I use string a=test.replace("//","/")
I get http:/www.test.com/web?testid=12 all with single slash(/) but I need http://www.test.com/web?testid=12.
I need only the second // nearby web, not first // near by www.
How to do this?
Upvotes: 0
Views: 367
Reputation: 223217
string test = @"http://www.test.com//web?testid=12";
test = test.Substring(0, test.LastIndexOf(@"//") - 1)
+ test.Substring(test.LastIndexOf(@"//")).Replace(@"//", @"/");
Or since its a Uri, you can do:
Uri uri = new Uri(test);
string newTest = uri.Scheme + @"//" + uri.Authority
+ uri.PathAndQuery.Replace(@"//",@"/");
Upvotes: 1
Reputation: 9261
var http = "http://someurl//data";
var splitindex = http.IndexOf("/") + 1;
var res = http.Substring(splitindex+1, (http.Length-1) - splitindex).Replace("//","/");
http = "http://" + res;
Or
StringBuilder strBlder = new StringBuilder();
strBlder.Append("http://someurl//data");
//use the previously used variable splitindex
strBlder.Replace("//", "/", splitindex + 1, (http.Length) - splitindex);
Upvotes: 0
Reputation: 2011
Simply remove one of the last slashes with String.Remove()
:
string test="http://www.test.com//web?testid=12";
string output = test.Remove(test.LastIndexOf("//"), 1);
Upvotes: 0
Reputation: 70
you can use stringbuilder as well.
StringBuilder b =new StringBuilder();
b.Replace("/","//",int startindex,int count);
Upvotes: 0
Reputation: 527
string test="http://www.test.com//web?testid=12"
string[] test2 = test.Split('//');
string test = test2[0] + "//" + test2[1] + "/" + test2[2];
Upvotes: 0
Reputation: 3261
You can make second replace
string test="http://www.test.com//web?testid=12";
string a=test.Replace("//","/").Replace("http:/","http://");
=)
Upvotes: 3