简体   繁体   English

分割逗号分隔的字符串得到“”

[英]Split comma separated string getting a “ ”

I have a comma separated string array like 我有一个逗号分隔的字符串数组

,/pictures/222.jpg,/pictures/inbannerbg2.jpg ,/ pictures / 222.jpg,/ pictures / inbannerbg2.jpg

While I split the array I am getting a " " value first.and it insert in to the table as null value. 在分割数组时,我首先得到一个“”值,并将其作为空值插入表中。 How can we remove this. 我们如何删除它。

here is my code 这是我的代码

build to comma sepparated array 生成逗号分隔的数组

Session["image"] = Session["image"] + ",/pictures/" + filename;
        img = Session["image"].ToString();

Split Array 分割阵列

img = Convert.ToString(Session["image"]);
        string[] img_split = img.Split(',');

Thanks in advance for help 预先感谢您的帮助

Use the method overload that allows you to specify a StringSplitOptions : 使用允许您指定StringSplitOptions的方法重载:

string[] img_split = img.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);

You can read about it in the docs on MSDN. 您可以在MSDN上的文档中阅读有关它的信息。

Since your string has a comma at the beginning of it, the first element of the array returned by Split() will be an empty string. 由于您的字符串开头有逗号,因此Split()返回的数组的第一个元素将是一个空字符串。 Here we tell it to drop the empty string. 在这里,我们告诉它删除空字符串。

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

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