分隔字符串并放置在数组中

时间:2018-09-19 02:26:33

标签: c#

如何通过分隔线或换行符将字符串拆分并放入数组中

这是api网址

 https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/boxscoreplayertrackv2

我尝试使用,但是字符串将其放置在索引0

     string[] lines = split.Split(
                new[] { Environment.NewLine },
                StringSplitOptions.None
            );

我想从字符串中获取每个单独的链接并存储在数组中

源链接:any-api.com

4 个答案:

答案 0 :(得分:2)

您也可以不带任何参数调用Split(),在这种情况下,它将拆分为空白字符,因此示例代码可以简单地简化为:

string[] lines = split.Split();

答案 1 :(得分:1)

var array = urlString.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

如果URL由多个空格分隔,则可以确保array中没有空白元素。

答案 2 :(得分:0)

这是给您的演示,您可以尝试一下。

string urlString = "https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/boxscoreplayertrackv2";
var array = urlString.Split(' ');

答案 3 :(得分:0)

根据您的情况,您可以像这样轻松地使用 Regex

string data = " https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/boxscoreplayertrackv2";
string regex = @"\s";
string[] result = Regex.Split(data.Trim(), regex);