给定以下字符串:
http://s.opencalais.com/1/pred/BusinessRelationType
我想要获取它的最后一部分:"BusinessRelationType"
我一直在考虑反转整个字符串,然后查找第一个"/",取左边的所有内容并反转。但是,我希望有更好/更简洁的方法。你有什么想法吗?
谢谢,保罗
关于字符串:
var stringUrl = "http://s.opencalais.com/1/pred/BusinessRelationType";
var lastPartOfUrl = stringUrl.Substring(stringUrl.LastIndexOf("/") + 1);
如果你将字符串转换为Uri:// 完全取决于您的需求。
var stringUrl = "http://s.opencalais.com/1/pred/BusinessRelationType";
var convertStringToUri = new Uri(stringUrl);
var lastPartOfUrl = convertStringToUri.PathAndQuery.Substring(convertStringToUri.AbsolutePath.LastIndexOf("/") + 1);
输出:
BusinessRelationType
也许你可以尝试做:
string x = "http://s.opencalais.com/1/pred/BusinessRelationType"
string.IsNullOrWhiteSpace(x)?x.Split('/').Last(): x,