我有字符串:"http://schemas.xmlsoap.org/ws/2004/09/transfer/Get".我想从最后一个斜线修剪一切,所以我只是留下来"Get".
var s = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get";
s = s.Substring(s.LastIndexOf("/") + 1);
Run Code Online (Sandbox Code Playgroud)
您可以使用LastIndexOf方法获取字符串中最后一个/的位置,并将其传递给Substring方法,作为要剪掉字符串的字符数.这应该让你最后得到Get.
[TestMethod]
public void ShouldResultInGet()
{
string url = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get";
int indexOfLastSlash = url.LastIndexOf( '/' ) + 1; //don't want to include the last /
Assert.AreEqual( "Get", url.Substring( indexOfLastSlash ) );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
943 次 |
| 最近记录: |