在下面的代码中,我希望字符串值以逗号分隔,但它会产生类似testtest1test2的结果。但是我想显示像test,test1,test2.Pls帮助我。
string Save=string.Empty;
for (int i = 0; i < files.Count; i++)
{
//i=0 test ,i=1 test1....
Save += nFIleUpload.OrgFileName;// display value like testtest1test2..
}
string s = string.Join(",", Save);
答案 0 :(得分:2)
更改此代码
string Save=string.Empty;
for (int i = 0; i < files.Count; i++)
{
Save += nFIleUpload.OrgFileName;// display value like testtest1test2..
}
string s = string.Join(",", Save);
以下
var Save = new List<string>();
for (int i = 0; i < files.Count; i++)
{
Save.Add(nFIleUpload.OrgFileName);// display value like testtest1test2..
}
var s = string.Join(",", Save);
答案 1 :(得分:1)
为了摆脱MethodMan的答案,当你厌倦了为每次迭代看到为Save添加相同的值时,请确保使用该索引。
var Save = new List<string>();
for (int i = 0; i < files.Count; i++)
{
Save.Add(nFIleUpload[i].OrgFileName);// display value like testtest1test2..
}
var s = string.Join(",", Save);
如果nFIleUpload实际上是保存字符串的变量..是文件变量吗?谁看着守望者?这些是要问的问题..
答案 2 :(得分:0)
尝试使用数组,示例:
string[] save = new string[files.Count];
for (var i = 0; i < files.Count; i++)
save[i] = nFIleUpload.OrgFileName;
// join all values
string s = string.Join(",", save);
或者使用stringBulder
更好地连接字符串,例如:
// define the stringBuilder
StringBuilder text = new StringBuilder();
// define the loop to concat all values separated by ',' comma
for (var i = 0; i < files.Count; i++)
text.AppendFormat("{0},", nFIleUpload.OrgFileName);
// get the output
string s = text.ToString();
// remove the last comma if necessary
if (s.EndsWith(","))
s = s.Remove(s.Length - 1);