Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi to all
I want to add \ charactor in between two strings.

Example : FirstString : One.
SecondString: Two


expected Output : One\Two.

Please Help me.
Posted
Updated 11-Mar-11 0:36am
v2
Comments
Tarun.K.S 11-Mar-11 6:38am    
Is it that difficult? You have earlier asked much tougher questions before.
satyagrahi_2010 11-Mar-11 7:24am    
hai tarun.
sorry to say. i lost somewhat concentration so thats way.
Take it easy.
Pravin Patil, Mumbai 11-Mar-11 7:20am    
I agree with Tarun...Earlier you had asked genuine questions...
What is the thing that you found so difficult in doing this..?
satyagrahi_2010 11-Mar-11 7:25am    
hai Pravin patil.
sorry to say. i lost somewhat concentration so thats way.
Take it easy.

C#
string first = "one";
string second = "two";

string result = string.Format(@"{0}\{1}", first, second);
 
Share this answer
 
v2
Comments
#realJSOP 11-Mar-11 7:44am    
Proposed as answer
fjdiewornncalwe 11-Mar-11 10:00am    
+5. The recommended way to concatenate strings together in C#.
Sergey Alexandrovich Kryukov 11-Mar-11 12:57pm    
Best way, so far, 5
--SA
As the others have said, the '+' operator is used for simple string concatenation and as Sunasara has demonstrated the '@' string literal enables you to use a more freeform style in your strings.

You may have encountered problems with the '\' character in your own attempts to solve this. Sunasara'a solution will work (apart from the fact that he shows a slash rather than a backslash), or you could use
C#
string c = a + "\\" + b;


Note the doubled backslash. The first is called an escape character and enables you to use characters in your strings that would otherwise be interpreted as keywords or control characters, such as the quote mark itself. Have a search for c# escape character for more information.
 
Share this answer
 
Tries following code.
String one = "One";
String two = "Two";
String both = one + "\\" + two;      //one for display and one for hiding meaning


When you want to add special characters in string its treated as special character so to hide the meaning of such special character by \
so,
writing n doesn't mean that its new line so we first hide the mean of n by \n that means new line character
 
Share this answer
 
Comments
fjdiewornncalwe 11-Mar-11 10:03am    
If you want to use the "+" operator, I would suggest that instead of "\\" as the string, use @"\" instead so that the actual action of the code is clearer to someone reading it.
String one = "One";
String two = "Two";
String both = one + "\" + two;


NB. This is fine for one or two concatinations, but if you need to do many/lots then use a StringBuilder.

Edit : Corrected the slash.
 
Share this answer
 
v2
Comments
Henry Minute 11-Mar-11 6:37am    
I think your slash is p*ssed.
use following:
C#
string a = "1";
string b = "2";
string c = a + @"/" + b;


Thanks,
Imdadhusen
 
Share this answer
 
Comments
satyagrahi_2010 11-Mar-11 6:36am    
Thanks Helped me
#realJSOP 11-Mar-11 9:01am    
Not as efficient as using string.Format.
Sergey Alexandrovich Kryukov 11-Mar-11 12:58pm    
Wrong slash.
--SA
string one = "One";
string two = "Two";
string delim = @"\";
string result = string.Join(delim, new string[] { one, two });
 
Share this 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