Click here to Skip to main content
15,908,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
which operator is used to concatenate strings in c#?
Posted
Comments
Drazen Pupovac 16-Dec-11 6:09am    
Do you think on this?

string c = "a" + "b";
RaviRanjanKr 16-Dec-11 15:10pm    
Simple Googling can solve your Problem.

In C#,
"+" operator is used to concatenate multiple strings.
C#
static void Main(string[] args)
{
    // To run this program, provide a command line string.
    // In Visual Studio, see Project > Properties > Debug.
    string userName = args[0];
    string date = DateTime.Today.ToShortDateString();

    // Use the + and += operators for one-time concatenations.
    string str = "Hello " + userName + ". Today is " + date + ".";
    System.Console.WriteLine(str);

    str += " How are you today?";
    System.Console.WriteLine(str);

    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
}

// Example output: 
//  Hello Alexander. Today is 1/22/2008.
//  Hello Alexander. Today is 1/22/2008. How are you today?
//  Press any key to exit.
//
 
Share this answer
 
v2
Comments
RaviRanjanKr 16-Dec-11 15:09pm    
5+
Use either
string str12 = str1 + str2
or
string str = string.Concat(str1, str2, ..., strN)
.
As this belongs to the fundamental basics of C# I recommend you reading some C# tutorials, otherwise you will have a lot more questions.
 
Share this answer
 
v2
Comments
RaviRanjanKr 16-Dec-11 15:09pm    
5+
C#
String.Concat("string1", "string2");

Alternatives
What's the best string concatenation method using C#?[^]
 
Share this answer
 
Comments
RaviRanjanKr 16-Dec-11 15:09pm    
5+
Very basic.
+(plus) is the concatenation operator.
string szVal = "hello" + "welcome"

or

you can use inbuild function String.Concat("string1", "string2");
 
Share this answer
 
Comments
RaviRanjanKr 16-Dec-11 15:09pm    
5+
simply use + operator to concat two strings

for ex.
string str1,str2;
string str12 = str1 + str2;
 
Share this answer
 
Comments
RaviRanjanKr 16-Dec-11 15:09pm    
5+
It's all explained here[^].
 
Share this answer
 
i have tried this but it didnt worrk. I have to concat two html contents
 
Share this answer
 
Comments
maajanes 16-Dec-11 6:40am    
i have tried this but it didnt worrk. I have to concat two html contents
Richard MacCutchan 16-Dec-11 7:04am    
Then you did it wrong. Show the actual code and the data variables that you used.
RaviRanjanKr 16-Dec-11 15:08pm    
A suggestion :- you can use Have a question or Comment button to drop your message and to get Immediate response instead of posting as answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900