Equivalent function to IO.Path.GetFileName for urls?

前端 未结 4 1216
情歌与酒
情歌与酒 2021-01-11 13:54

In .NET 4.0, whats the equivalent function to IO.Path.GetFileName for urls?

4条回答
  •  长发绾君心
    2021-01-11 14:21

    You may stick to creating Uri object or if you care about performance use something similar to this one:

        public class UriHelpers
        {
            public static string GetFileNameFromUrl(string url)
            {
                string lastSegment = url.Split('/').Last();
    
                return lastSegment.Substring(0, lastSegment.IndexOf('?') < 0 ? lastSegment.Length : lastSegment.IndexOf('?'));
            }
        }
    

提交回复
热议问题