繁体   English   中英

删除字符串数组中存在的字符串部分

[英]Remove the part of a string that exists in an array of strings

我有一个字符串路径名和一个包含国家扩展名的字符串数组。

string url = "/pl/test-page/index.html";
string[] countryExtensions = {"/pl/", "/de/", "/tr/"};

如果给定数组中存在,我想要实现的是删除该部分(示例中的 /pl/)。

如果存在,我只想接收以下 url 而无需重复;

string newUrl = "/test-page/index.html";

使用新的 url 我只想打印一些元代码,如下所示。 我尝试使用 foreach 但它会重复相同的元代码。

<link rel="alternate" hreflang="en-US" href="https://example.com/test-page/index.html" />
<link rel="alternate" hreflang="pl-PL" href="https://example.com/pl/test-page/index.html" />
<link rel="alternate" hreflang="de-DE" href="https://example.com/de/test-page/index.html" />
<link rel="alternate" hreflang="tr-TR" href="https://example.com/tr/test-page/index.html" />

我怎样才能做到这一点?

这给了你你所要求的,但如果它是你想要的,这似乎有点奇怪:

        string url = "/pl/test-page/index.html";
        string[] ext = {"/pl/", "/de/", "/tr/"};

        //remove the /pl/ and replace with /
        url = url.Replace(ext.FirstOrDefault(x => url.StartsWith(x)) ?? "/", "/");

        //build a list of urls /test.., /pl/test.., /de/test.. and /tr/test..
        var urls = new[] { url }.Concat(ext.Select(x => x.TrimEnd('/') + url));

您可以使用这些urls来构建您的 HTML(我没有在“https://example.com”前面加上前缀)

暂无
暂无

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

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