有没有在C#中清除URI的方法?

有没有在C#中清除URI的方法?,c#,parsing,url,rtsp,C#,Parsing,Url,Rtsp,程序中应包含以下URI: rtsp://127.0.0.1:554/live.sdp/ rtsp://127.0.0.1:554/live.sdp rtsp://127.0.0.1:554 rtsp://127.0.0.1:554/ rtsp://127.0.0.1:554//live.sdp 最后,我只想有一个干净的方法: rtsp://127.0.0.1:554/live.sdp 其中,方案(rtsp://)、ip(127.0.0.1)、端口(554)和路径(live.sdp)分别提交。

程序中应包含以下URI:

rtsp://127.0.0.1:554/live.sdp/
rtsp://127.0.0.1:554/live.sdp
rtsp://127.0.0.1:554
rtsp://127.0.0.1:554/
rtsp://127.0.0.1:554//live.sdp
最后,我只想有一个干净的方法:

rtsp://127.0.0.1:554/live.sdp
其中,方案(rtsp://)、ip(127.0.0.1)、端口(554)和路径(live.sdp)分别提交。因此,我必须确保路径不仅包含斜杠(/),不以斜杠(/live.sdp)开头,不以斜杠(live.sdp/)结尾,而且URL不以斜杠结尾(rtsp://127.0.0.1:554/)因此,我可以在URL和路径之间添加斜杠,而不用担心出错


有什么简单的方法可以实现这一点吗?

使用该类来解析和构造URI。无需重新发明轮子。

尝试使用
字符串拆分和格式化

string s1 = ip + ":" + port + "/" + path;
string[] arr = s1.Split(new string[] { "/" }, 
                        StringSplitOptions.RemoveEmptyEntries);

string url = scheme + string.Format("{0}/{1}", arr)

试试这样的

var uri = new UriBuilder(String.Format("{0}://{1}:{2}/{3}",protocol,host,port,path)).Uri;

您可以先将
URI
字符串按字符
/
拆分,然后逐个获取它们。这是我知道的最简单的。