最佳答案
当这看起来很简单,有很多关于字符串/字符/正则表达式的问题,但是我找不到我需要的东西(除了在另一种语言: 删除特定点后的所有文本中)时,我觉得发布这个有点傻。
我有以下密码:
[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);
}
我尝试了上面问题中的解决方案(希望语法是相同的!)但没有。我想首先删除 queryString,它可以是任意长度,然后删除页面名,它也可以是任意长度。
如何从完整 URL 中删除查询字符串,以使该测试通过?