strcat in c#?

R

Retro

Hello

Say i have two varibles one name var1 and the other var2,
var1 holds the string "C:\" and var2 holds "games" is there a command to
combian the two?

When i use C++ i could use the command strcat(var1, var2);

Is there a command like "strcat" in c#?
 
R

Raj Chudasama

use
var1+var2

:)

its overloaded in the string class check out string class for more
functions.
 
R

Reginald Blue

Retro said:
Hello

Say i have two varibles one name var1 and the other var2,
var1 holds the string "C:\" and var2 holds "games" is there a command
to combian the two?

When i use C++ i could use the command strcat(var1, var2);

Well, of course, in C++ you could also use the string class in the standard
library:

std::string var1 = "C:\\";
std::string var2 = "games";

std::string var3 = var1+var2;

In C# the System.String class behaves the exact same way:

System.String var1 = "C:\\";
System.String var2 = "games";

System.String var3 = var1 + var2;
Is there a command like "strcat" in c#?

Now, if you really mean that you want to control it at this low a level,
there is the StringBuilder class as well which will allow you more direct
control of the string buffers.

System.Text.StringBuilder sb1 = new System.Text.StringBuilder("C:\\");

sb1.Append(var2);

This will have an effect which is very close to the way that strcat works.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
 
G

Guest

System.Text.StringBuilder is more efficient. Somehing like...

string var1 = @"c:\";
string var2 = "games";
System.Text.StringBuilder var3 = System.Text.StringBuilder();

var3.Append(var1);
var3.Append(var2);
 
R

Retro

thank you very much for all your help


Reginald Blue said:
Retro said:
Hello

Say i have two varibles one name var1 and the other var2,
var1 holds the string "C:\" and var2 holds "games" is there a command
to combian the two?

When i use C++ i could use the command strcat(var1, var2);

Well, of course, in C++ you could also use the string class in the
standard library:

std::string var1 = "C:\\";
std::string var2 = "games";

std::string var3 = var1+var2;

In C# the System.String class behaves the exact same way:

System.String var1 = "C:\\";
System.String var2 = "games";

System.String var3 = var1 + var2;
Is there a command like "strcat" in c#?

Now, if you really mean that you want to control it at this low a level,
there is the StringBuilder class as well which will allow you more direct
control of the string buffers.

System.Text.StringBuilder sb1 = new System.Text.StringBuilder("C:\\");

sb1.Append(var2);

This will have an effect which is very close to the way that strcat works.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
 
J

Jon Skeet [C# MVP]

Raj Chudasama said:
use
var1+var2

:)

its overloaded in the string class check out string class for more
functions.

No, it's not overloaded in the String class. The C# compiler knows
about strings and calls String.Concat(...) in the generated code.
 
J

Jon Skeet [C# MVP]

carion1 said:
System.Text.StringBuilder is more efficient. Somehing like...

string var1 = @"c:\";
string var2 = "games";
System.Text.StringBuilder var3 = System.Text.StringBuilder();

var3.Append(var1);
var3.Append(var2);

That's *less* efficient than:

string var3 = var1 + var2;

It's significantly less efficient if var1 and var2 are actually
constants, as then the compiler does the concatenation, leaving nothing
to do at runtime.
 
D

Daniel Jin

carion1 said:
System.Text.StringBuilder is more efficient. Somehing like...

it's more efficient only when used appropriately, which this is not the
case here.

to op, the equivalent of strcat in .NET is String.Concat( s1, s2 ),
which as Jon pointed out, is what s3 = s1 + s2 compiled to by the
compiler.
 
J

Jon Skeet [C# MVP]

Daniel Jin said:
it's more efficient only when used appropriately, which this is not the
case here.

to op, the equivalent of strcat in .NET is String.Concat( s1, s2 ),
which as Jon pointed out, is what s3 = s1 + s2 compiled to by the
compiler.

It's definitely time to write a C# equivalent of
http://www.pobox.com/~skeet/java/stringbuffer.html

I've seen misinformation about this crop up so often... I'll see what I
can do tonight.
 
J

John Murray

Retro said:
Hello

Say i have two varibles one name var1 and the other var2,
var1 holds the string "C:\" and var2 holds "games" is there a command to
combian the two?

When i use C++ i could use the command strcat(var1, var2);

Is there a command like "strcat" in c#?


Aside from the discussion on the relative efficiency of Concat and a
StringBuilder ... in this case, you may want to look at
System.IO.Path.Combine ... which combines path segments ...and has some
other nice features as well...
 
C

Cool Guy

Jon Skeet said:

Nice one. I especially liked the shopping list analogy.

Maybe you could also point out that, after all,

s += t;

is the same as

s = s + t;

.. That might aid understanding even more.

Also, maybe an example could clarify this quote:

| If you need the intermediate results of the concatenation
| for something other than feeding the next iteration of
| concatenation, StringBuilder isn't going to help you.

Something like the following perhaps.

string fullName = firstName + " " + lastName;
string details = fullName + " - " + phoneNumber;

Just a thought.
 
J

Jon Skeet [C# MVP]

Cool Guy said:
Nice one. I especially liked the shopping list analogy.

Thanks - I was pleased with that, too - I hadn't thought of it before.
Maybe you could also point out that, after all,

s += t;

is the same as

s = s + t;

. That might aid understanding even more.

Good plan.
Also, maybe an example could clarify this quote:

| If you need the intermediate results of the concatenation
| for something other than feeding the next iteration of
| concatenation, StringBuilder isn't going to help you.

Something like the following perhaps.

string fullName = firstName + " " + lastName;
string details = fullName + " - " + phoneNumber;

Just a thought.

Right. I'll need to think about how best to do that. An actual code
example would be a bit odd in the middle of the list. Maybe I need to
explain it more in the preceding text...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top