C#将字符串添加到另一个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12247147/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:36:48  来源:igfitidea点击:

C# adding string to another string

c#

提问by user1639776

Possible Duplicate:
What's the best string concatenation method using C#?

可能的重复:
使用 C# 的最佳字符串连接方法是什么?

I have a variable :

我有一个变量:

string variable1;

And I'm trying to make something like this :

我正在尝试做这样的事情:

for (int i = 0; i < 299; i += 2)
        {
            variable1 = variable1 && IntToHex(buffer[i]);
        }

IntToHex is a string function, so the result of the "IntToHex(buffer[i])" will be string. But it comes up to an error saying I cannot use &&. Is there any solution to add a string to another string? Thanks!

IntToHex 是一个字符串函数,因此“IntToHex(buffer[i])”的结果将是字符串。但是出现了一个错误,说我不能使用&&。有没有办法将一个字符串添加到另一个字符串中?谢谢!

采纳答案by ChrisF

Just use the +operator:

只需使用+运算符:

variable1 = variable1 + IntToHex(buffer[i]);

You also need to initialise variable1:

您还需要初始化variable1

string variable1 = string.Empty;

or

或者

string variable1 = null;

However consider using a StringBuilderinstead as it's more efficient:

但是考虑使用 aStringBuilder代替,因为它更有效:

StringBuilder builtString = new StringBuilder();

for (int i = 0; i < 299; i += 2)
{
    builtString.Append(IntToHex(buffer[i]));
}

string variable1 = builtString.ToString();

回答by Sani Singh Huttunen

variable1 = variable1 + IntToHex(buffer[i]);

However, this is probably better:

但是,这可能更好:

var sb = new StringBuilder();
for (int i = 0; i < 299; i += 2)
    {
        sb.Append(IntToHex(buffer[i]));
    }
variable1 = sb.ToString();

回答by Henk Holterman

In C#, simply use a +to concatenate strings:

在 C# 中,只需使用 a+来连接字符串:

  variable1 = variable1 + IntToHex(buffer[i]);

But more important, this kind of situation requires a StringBuilder.

但更重要的是,这种情况需要一个StringBuilder。

    var buffer = new StringBuilder();
    for (int i = 0; i < 299; i += 2)
    {
        buffer.Append( IntToHex(buffer[i]) );
    }

    string variable1 = buffer.ToString();

For loops of 100 or more this really makes a big difference in performance.

对于 100 个或更多的循环,这确实会对性能产生很大的影响。

回答by lebryant

variable1 += IntToHex(buffer[i]);

回答by Mutu Yolbulan

there are various ways of adding a string to a string. you can use a simple + function which is not recommended in most cases. string.concat and string.format are preferred methods of adding strings. also stringBuilder class is useful in adding large portions of strings together

有多种方法可以将字符串添加到字符串中。您可以使用在大多数情况下不推荐的简单 + 函数。string.concat 和 string.format 是添加字符串的首选方法。stringBuilder 类也可用于将大部分字符串添加在一起

回答by Anirudha

You need to use +for concatenating strings in c#

您需要+在 c# 中用于连接字符串

for (int i = 0; i < 299; i += 2)
{
   variable1 = variable1 + IntToHex(buffer[i]);
}

But StringBuilder would be a good option here...

但 StringBuilder 在这里将是一个不错的选择......

StringBuilder sb = new StringBuilder("");

for (int i = 0; i < 299; i += 2)
{
   sb= sb.Append(IntToHex(buffer[i]));
}

回答by Jon Skeet

&&is the conditional-AND operator.

&&条件与运算符

You canuse the +operator for string concatenation, but it's not a good idea to use that within a loop (details).

可以+运算符用于字符串连接,但在循环(details) 中使用它不是一个好主意。

Either use a StringBuilder:

要么使用StringBuilder

StringBuilder builder = new StringBuilder(299 * 4); // Or whatever
for (int i = 0; i < 299; i += 2)
{
    builder.Append(IntToHex(buffer[i]));
}
string combined = builder.ToString();

Or potentially use string.Join- it might not be as practical in this case given your looping, but in other cases it would be great. You could still use it here, like this:

或者可能使用string.Join- 在这种情况下,考虑到您的循环,它可能不那么实用,但在其他情况下它会很棒。你仍然可以在这里使用它,就像这样:

string combined = string.Join("", Enumerable.Range(0, 149)
                                       .Select(i => IntToHex(buffer[i * 2])));