Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中拆分字符串URL_C#_Arrays_Regex_String - Fatal编程技术网

C# 在c中拆分字符串URL

C# 在c中拆分字符串URL,c#,arrays,regex,string,C#,Arrays,Regex,String,有人能帮我吗?我有一个包含多个URL的字符串 如何将这些URL拆分为字符串数组 尝试: 为了完整性,您可以使用正则表达式: var urls = "https://stackoverflow.com/questions/ask.csvhttp://stackoverflow.com/questions/ask1.csvhttps://stackoverflow.com/questions/ask3.csv"; var urlsArray = Regex.Split(urls, "(?=h

有人能帮我吗?我有一个包含多个URL的字符串 如何将这些URL拆分为字符串数组

尝试:

为了完整性,您可以使用正则表达式:

  var urls = "https://stackoverflow.com/questions/ask.csvhttp://stackoverflow.com/questions/ask1.csvhttps://stackoverflow.com/questions/ask3.csv";
  var urlsArray = Regex.Split(urls, "(?=https?://)").Where(i => !string.IsNullOrEmpty(i)).ToArray();
模式?=https?://说明:


这是一个积极的前瞻,它确保了下面的内容是带有可选s和://的http。

如果它们都是https://的话,我可以使用一个简单的拆分:

var a = urls.Split(new[]{"https://"}, StringSplitOptions.None).Select(url => "https://" + url);
但是您可以使用regex,如果混合使用http/s,这会很有帮助。regex周围的括号导致保留分隔符,但这会生成一个{scheme,host/path,scheme,host/path}数组,因此需要做一些工作才能将这些方案粘回到主机/路径上

string[] result = Regex.Split(urlsasstring, @"(https?://)");
string[] urls = new string[result.Length/2];
for(int x = 0, y=0; x < result.Length; x+= 2,y++){
  urls[y] = result[x] + result[x+1];
}

或者您可以使用indexof和substring自己跳过它。令人讨厌的是,没有一个IndexOfAny为http/https获取字符串数组,但您可以执行http,然后为://执行IndexOf,并查看它在第一个IndexOf之后是4还是5,如果不是,则跳过。每次找到一个新的有效索引时,都要调用以前的索引和它们之间的子字符串。我想这很复杂。。使用urlsasstring.Replace将http://替换为url选项卡中不显示的单个字符可能更容易些?和https://另一个不出现在url换行符中的字符?然后使用IndexOfAny,然后将其转换回子字符串过程中的一部分

您自己尝试过什么吗?你做了什么研究,我猜你在分割字符串方面做了什么?为什么它不适用于你的情况?为什么URL存储为没有分隔符的单个字符串?这就是从第三方服务获取字符串的方式。如果有一个分隔符,我会使用Split方法……我的其他问题的答案是什么?@Axya Split使用https://and 请在每个项目前面加上https://前缀。我认为您需要说明为什么在https://上拆分是不可接受的,然后再将其添加回。。我完全同意将其作为解决方案,这比在url的其他部分拆分要好。csv可能更合理地出现在url的其他部分,其中有一个https://www.myhttpwebsite.com?No,拆分方法将StringSplitOptions作为字符串的第二个参数时出现编译器错误。@Axya它在我的PC上工作,另请参见更新:@Axya然后您应该接受答案左侧的绿色复选标记。
var a = urls.Split(new[]{"https://"}, StringSplitOptions.None).Select(url => "https://" + url);
string[] result = Regex.Split(urlsasstring, @"(https?://)");
string[] urls = new string[result.Length/2];
for(int x = 0, y=0; x < result.Length; x+= 2,y++){
  urls[y] = result[x] + result[x+1];
}