如何在c#.net中添加两个字符串 [英] how to add two strings in c#.net

查看:59
本文介绍了如何在c#.net中添加两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我想在两个字符串之间添加\字符.

示例:FirstString:一个.
SecondString:两个


预期输出:One\Two.

请帮助我.

Hi to all
I want to add \ charactor in between two strings.

Example : FirstString : One.
SecondString: Two


expected Output : One\Two.

Please Help me.

推荐答案

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

string result = string.Format(@"{0}\{1}", first, second);


" +"运算符用于简单字符串串联,并且如Sunasara所演示的那样,"@"字符串文字使您可以在字符串中使用更自由的样式.

您自己尝试解决此问题时可能遇到了"\"字符问题. Sunasara''解决方案将起作用(除了他显示斜杠而不是反斜杠的事实),或者您可以使用
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
string c = a + "\\" + b;



注意加倍的反斜杠.第一个称为escape character,使您可以使用字符串中的字符,否则这些字符将被解释为关键字或控制字符,例如引号本身.搜索 c#转义字符以获取更多信息.



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.


请尝试以下代码.
Tries following code.
String one = "One";
String two = "Two";
String both = one + "\\" + two;      //one for display and one for hiding meaning



当您想在字符串中添加特殊字符时,将其视为特殊字符,以便用\
隐藏此类特殊字符的含义. 所以,
写n并不意味着换行,所以我们首先用\ n隐藏n的平均值,这意味着换行字符



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


这篇关于如何在c#.net中添加两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆