c# - 如何分隔字符串,c#

标签 c# xml string youtube

我在从该字符串中获取“关键字”时遇到问题,我尝试了 string.replace() 但它没有用,有人知道如何将关键字与该字符串分开吗?

var url = "< id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword< /id>";

感谢您的帮助!

最佳答案

当您使用 xml 文档时,很容易获取值元素:

var xml = "<id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword</id>";
var url = XElement.Parse(xml).Value;

var index = url.LastIndexOf('/') + 1;
var keyword = url.Substring(index);

如果您总是只需要最后一段,您可以使用 Url 实例轻松实现:

var keyword = new Uri(url).Segments.Last();

感谢@Alexei

关于c# - 如何分隔字符串,c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19746387/

相关文章:

c# - 删除空白段落标签 - TinyMCE

xml - 读取/写入包含特殊字符的 XML 节点

c++ - 在 C++ 中拆分字符串

string - 限制 VB.NET 中 List(Of String) 中的元素数量

c - Strtok 分隔所有空格

c# - 使用主页时在网页上设置背景图片

c# - ResourceDictionary : {"Key cannot be null.\r\nParameter name: key"} 中的运行时错误

XML验证: 'No matching global declaration available for the validation root'

c# - 是否可以在 C# 5.0 中使用相同的异步方法签名并能够稍后决定如何执行此类方法

java - 在 Java 中解析伪 XML 文件