C# ForEach 字符串连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/997455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ForEach String concat
提问by
A little newbie question. I have a foreach loop where I am trying to access the property of a row object and assign to a string.
一个小菜鸟问题。我有一个 foreach 循环,我试图访问一个行对象的属性并分配给一个字符串。
foreach(row in Dataset)
{
string finalName= row.name;
}
On each iteration I need to concat each row's name property to the finalName
在每次迭代中,我需要将每一行的 name 属性连接到 finalName
\Thanks alot,
\非常感谢,
回答by Martin Harris
Depending on your performance requirements you can either concat using the addition operator:
根据您的性能要求,您可以使用加法运算符进行连接:
string finalName = string.Empty;
foreach(row in Dataset)
{
finalName += row.name;
}
or use a StringBuilder:
或使用StringBuilder:
Stringbuilder sb = new StringBuilder();
foreach(row in Dataset)
{
sb.Append(row.Name);
}
string finalName = sb.ToString();
General for very small numbers of appends you won't notice a difference between the two versions. But if you are combining a lot of strings then there may be a very noticeable performance and memory benefit to using the StringBuilder.
一般对于非常少量的附加,您不会注意到两个版本之间的差异。但是,如果您要组合很多字符串,那么使用 StringBuilder 可能会带来非常显着的性能和内存优势。
Also bear in mind that this will place the strings directly one after the other so you may also wish to append a space or new line between each of them.
还要记住,这会将字符串直接一个接一个放置,因此您可能还希望在每个字符串之间添加一个空格或新行。
Special bonus LINQ one-liner:
LINQ 单线特别加分:
Since you seem to be new to C# I'd suggest that you ignore this one since it is quite advanced and frankly not as readable (but on the other hand who doesn't like throwing random bits of LINQ into their code?). Still, I'll add it here for completeness. I don't know what the performance is like, but one major advantage over the other methods is the ease with with you can place a space or other character between the strings. Just change the value in the marked line to whatever value you want to separate the combined strings with (Environment.NewLine or " ", for example)
由于您似乎是 C# 的新手,我建议您忽略它,因为它非常先进,而且坦率地说不那么可读(但另一方面,谁不喜欢将 LINQ 的随机位扔到他们的代码中?)。不过,为了完整起见,我会在此处添加它。我不知道性能如何,但与其他方法相比的一个主要优势是您可以轻松地在字符串之间放置空格或其他字符。只需将标记行中的值更改为您想用(例如,Environment.NewLine 或“”)分隔组合字符串的任何值
string finalName = string.Join(
"", //Change the value here to separate the strings.
(from row in Dataset
select row.Name).ToArray());
or, if you prefer lambdas to LINQ:
或者,如果您更喜欢 lambdas 而不是 LINQ:
string finalName = string.Join(
"", //Change the value here to separate the strings.
Dataset.Select(row => row.Name).ToArray());
回答by curtisk
If you really just want to concat, then this is all you need...
如果你真的只想连接,那么这就是你所需要的......
string finalName = "";
foreach(DataRow row in dataset.Tables[tablename].Rows)
{
finalName += row.name;
}
回答by Austin Salonen
Here's one...
这是一个...
string finalName = ""
foreach (DataRow row in dataSet.TableX)
{
finalName += row.Name;
finalName += Environment.NewLine;
}
Here's another...
这是另一个...
StringBuilder finalName = new StringBuilder()
foreach (DataRow row in dataSet.TableX)
{
finalName.Append(row.Name);
finalName.Append(Environment.NewLine);
}
回答by Adam Robinson
Just a couple of things...
只是几件事......
DataSet
's don't have rows, they haveDataTable
s, and thoseDataTable
s haveDataRows
- You can use the
+=
operator to perform a concatenation, though the general advice is that aStringBuilder
object is better suited to this for performance and memory reasons.
DataSet
's 没有行,他们有DataTable
s,而那些DataTable
s 有DataRows
- 您可以使用
+=
运算符来执行串联,但一般建议是,StringBuilder
出于性能和内存方面的原因,对象更适合于此。
Assuming your DataSet
has one DataTable
and the column you're after is called name
, you can use either of these (the first using the +=
operator, the second using a StringBuilder
)
假设您DataSet
有一个DataTable
并且您所追求的列被调用name
,您可以使用其中任何一个(第一个使用+=
运算符,第二个使用 a StringBuilder
)
string finalName = string.Empty;
foreach(DataRow row in dataSet.Tables[0].Rows)
{
finalName += row["name"].ToString();
}
or
或者
System.Text.StringBuilder builder = new System.Text.StringBuilder();
foreach(DataRow row in dataSet.Tables[0].Rows)
{
builder.Append(row["name"].ToString());
}
string finalName = builder.ToString();
回答by KMR
you can add the values into a list and then join the values in the list
您可以将值添加到列表中,然后加入列表中的值
List<string> listRows = new List<string>();
string rowValues = string.Empty;
foreach(row in DataSet)
{
string fileName = row.name;
listRows.Add(fileName);
}
rowValues = string.Join(",", listRows);