简体   繁体   English

如何从列表对象中删除空字符串

[英]how to remove empty strings from list object

Before I set Url = r.Link I want to make sure r.link is not empty string?在设置 Url = r.Link 之前,我想确保 r.link 不是空字符串?

Would I be able to do that in this code snippet?我能在这个代码片段中做到这一点吗?

target.RelatedArtifact = template.References.Select(r => new RelatedArtifact()
            {
                Type = r == template.References.First() ? RelatedArtifact.RelatedArtifactType.DerivedFrom : RelatedArtifact.RelatedArtifactType.Citation,
                Display = StripHtml(r.Text).Replace(" .", ".").Replace(" ®", "®") /* Html stripping artifact in <em></em> */,
                Url = r.Link
            }).ToList();

Output输出

<relatedArtifact>
    <type value="citation" />
    <display value="sss, et al. ss. s;2(2):2." />
    <url value="" /> shouldn't have this tag?
</relatedArtifact>

very simply, test r.Link and replace it with whatever you need if is null or empty (or just white space, I assume)非常简单,测试 r.Link 并用你需要的任何东西替换它,如果它是空的或空的(或者只是空白,我假设)

target.RelatedArtifact = template.References.Select(r => new RelatedArtifact()
            {
                Type = r == template.References.First() ? RelatedArtifact.RelatedArtifactType.DerivedFrom : RelatedArtifact.RelatedArtifactType.Citation,
                Display = StripHtml(r.Text).Replace(" .", ".").Replace(" ®", "®") /* Html stripping artifact in <em></em> */,
                Url = string.IsNullOrWhiteSpace(r.Link)? SOME_MEANINGFUL_VALUE : r.Link
            }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM