Remove last segment of Request.Url

前端 未结 5 646
旧巷少年郎
旧巷少年郎 2021-01-07 22:59

I would like to remove the last segment of Request.Url, so for instance...

http://www.example.com/admin/users.aspx/deleteUser

5条回答
  •  一生所求
    2021-01-07 23:59

    Well the trivial solution would be to iterate char by char from the end of the string towards its beginning and search for the first '/' to come (I guess that also came into your mind).

    Try this:

    string url = "http://www.example.com/admin/users.aspx/deleteUser";
    
    for (int i = url.Length - 1; i >= 0; i--) {
    
        if (url[i] == '/') return url.Substring(0, i - 1);
    }
    

提交回复
热议问题