9

Possible Duplicate:
What's the best string concatenation method using 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!

Community
  • 1
  • 1
user1639776
  • 149
  • 2
  • 3
  • 7

7 Answers7

14

Just use the + operator:

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

You also need to initialise variable1:

string variable1 = string.Empty;

or

string variable1 = null;

However consider using a StringBuilder instead as it's more efficient:

StringBuilder builtString = new StringBuilder();

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

string variable1 = builtString.ToString();
ChrisF
  • 134,786
  • 31
  • 255
  • 325
4

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

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

But more important, this kind of situation requires a 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.

H H
  • 263,252
  • 30
  • 330
  • 514
3

&& is the conditional-AND operator.

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

Either use a 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 combined = string.Join("", Enumerable.Range(0, 149)
                                       .Select(i => IntToHex(buffer[i * 2])));
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2
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();
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
2
variable1 += IntToHex(buffer[i]);
lebryant
  • 351
  • 2
  • 18
1

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

Mutu Yolbulan
  • 1,052
  • 1
  • 8
  • 21
1

You need to use + for concatenating strings in c#

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

But StringBuilder would be a good option here...

StringBuilder sb = new StringBuilder("");

for (int i = 0; i < 299; i += 2)
{
   sb= sb.Append(IntToHex(buffer[i]));
}
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Anirudha
  • 32,393
  • 7
  • 68
  • 89