可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I feel kind of dumb posting this when this seems kind of simple and there are tons of questions on strings/characters/regex, but I couldn't find quite what I needed (except in another language: Remove All Text After Certain Point).
I've got the following code:
[Test] public void stringManipulation() { String filename = "testpage.aspx"; String currentFullUrl = "http://localhost:2000/somefolder/myrep/test.aspx?q=qvalue"; String fullUrlWithoutQueryString = currentFullUrl.Replace("?.*", ""); String urlWithoutPageName = fullUrlWithoutQueryString.Remove(fullUrlWithoutQueryString.Length - filename.Length); String expected = "http://localhost:2000/somefolder/myrep/"; String actual = urlWithoutPageName; Assert.AreEqual(expected, actual); }
I tried the solution in the question above (hoping the syntax would be the same!) but nope. I want to first remove the queryString which could be any variable length, then remove the page name, which again could be any length.
How can I get the remove the query string from the full URL such that this test passes?
回答1:
For string manipulation, if you just want to kill everything after the ?, you can do this
string input = "http://www.somesite.com/somepage.aspx?whatever"; int index = input.IndexOf("?"); if (index > 0) input = input.Substring(0, index);
Edit: If everything after the last slash, do something like
string input = "http://www.somesite.com/somepage.aspx?whatever"; int index = input.LastIndexOf("/"); if (index > 0) input = input.Substring(0, index); // or index + 1 to keep slash
Alternately, since you're working with a URL, you can do something with it like this code
System.Uri uri = new Uri("http://www.somesite.com/what/test.aspx?hello=1"); string fixedUri = uri.AbsoluteUri.Replace(uri.Query, string.Empty);
回答2:
To remove everything before first "/"
input = input.Substring(input.IndexOf("/"));
To remove everything after first "/"
input = input.Substring(0, input.IndexOf("/")+1);
To remove everything before last "/"
input = input.Substring(input.LastIndexOf("/"));
To remove everything after last "/"
input = input.Substring(0, input.LastIndexOf("/")+1);
Even more simpler option for removing characters after specified char is to use string.Remove() method as follows:
To remove everything after first "/"
input = input.Remove(input.IndexOf("/")+1);
To remove everything after last "/"
input = input.Remove(input.LastIndexOf("/")+1);
回答3:
The Uri class is generally your best bet for manipulating Urls.
回答4:
Request.QueryString
helps you to get the parameters
and values included within the URL
example
string http = "http://dave.com/customers.aspx?customername=dave" string customername = Request.QueryString["customername"].ToString();
so the customername variable should be equal to dave
regards
回答5:
I second Hightechrider: there is a specialized Url class already built for you.
I must also point out, however, that the PHP's replaceAll uses regular expressions for search pattern, which you can do in .NET as well - look at the RegEx class.
回答6:
To remove everything before a specific char, use below.
string1 = string1.Substring(string1.IndexOf('$') + 1);
What this does is, takes everything before the $ char and removes it. Now if you want to remove the items after a character, just change the +1 to a -1 and you are set!
But for a URL, I would use the built in .NET class to take of that.
回答7:
you can use .NET's built in method to remove the QueryString
. i.e., Request.QueryString.Remove["whatever"];
here whatever in the [ ] is name of the querystring
which you want to remove.
Try this... I hope this will help.