473,395 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

strcat in c#?

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#?

Nov 17 '05 #1
14 11542
use
var1+var2

:)

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

"Retro" <re***@indovirus.net> wrote in message
news:dg*******************@news.demon.co.uk...
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#?

Nov 17 '05 #2
Retro wrote:
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]
Nov 17 '05 #3
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);

"Raj Chudasama" wrote:
use
var1+var2

:)

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

"Retro" <re***@indovirus.net> wrote in message
news:dg*******************@news.demon.co.uk...
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#?


Nov 17 '05 #4
thank you very much for all your help
"Reginald Blue" <Re***********@hotmail.com> wrote in message
news:dg**********@trsvr.tr.unisys.com...
Retro wrote:
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]

Nov 17 '05 #5
Raj Chudasama <ra*@asteriasgi.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Ooppps sorry i am wrong then :(

should do my HW

thanks jon

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Raj Chudasama <ra*@asteriasgi.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7
carion1 <ca*****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
carion1 wrote:
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.

Nov 17 '05 #9
Daniel Jin <sh********@yahoo.com> wrote:
carion1 wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
Nobody caught the missing 'new' at least!

"Jon Skeet [C# MVP]" wrote:
Daniel Jin <sh********@yahoo.com> wrote:
carion1 wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #11
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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.


As promised:
http://www.pobox.com/~skeet/csharp/stringbuilder.html

Comments welcome, as ever.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12
Retro wrote:
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...
Nov 17 '05 #13
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
As promised:
http://www.pobox.com/~skeet/csharp/stringbuilder.html

Comments welcome, as ever.


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.
Nov 17 '05 #14
Cool Guy <co*****@abc.xyz> wrote:
As promised:
http://www.pobox.com/~skeet/csharp/stringbuilder.html

Comments welcome, as ever.
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Patrick Coleman | last post by:
Hi, I have the following code: char request = "GET "; strcat(request, path); //Path is the path section of a url ie. "/path/test.htm" strcat(request, " HTTP/1.1"); cout<<request<<"\n";...
9
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
5
by: Kevin C. | last post by:
Never mind my last post about pointer subtraction, I traced the code and found the offender to be strcat. char call = "del "; system(strcat(strcat(call, del->table_name), ".tab")); After this...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
8
by: ctara_shafa | last post by:
Hi, I have a following problem: I'm creating a list and one of the fields should contain the date. Firstly I ask the user for the year, month and day and then I'd like to collect all this data in...
9
by: cicalese | last post by:
For starters, I am a C novice. I don't fully understand how strings work. I am running on a linux box. I compile w/ gcc. I have a string called 'myabqcommand' that is being overwritten when i use...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
4
by: nick048 | last post by:
Hi, I have this problem: int n; char nToChar; char firstString = "The number is: "; char resp; /* HERE MAIN WITH THE INPUT OF n*/
3
by: sail0r | last post by:
Perhaps this is obvious but I am not sure what is going on... Here is the relevant code: char *command; char *argument; char url="file:///usr/u/myname/Project/cats/"; char...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.