将两个字符串与转义序列连接 [英] Concatenating two strings with escape sequences

查看:48
本文介绍了将两个字符串与转义序列连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#将两个字符串与转义序列连接起来,而我想跳过这些转义序列,因此我在每个字符串之前使用@符号.看起来像这样:

  string firstString = @使用\ n作为换行符.";字符串secondString = @将\ b用于退格";返回firstString + secondString; 

问题是:返回值中是否会跳过转义序列?;

解决方案

其他答案当然是正确的.为了清楚;

这在C#规范的部分中进行了介绍:

2.4.4.5字符串文字

C#支持两种形式的字符串文字:常规字符串文字和逐字字符串文字.

常规字符串文字由零个或多个用双引号引起来的字符组成,例如在"hello"中,并且可以包括简单的转义序列(例如制表符\ t)以及十六进制和Unicode转义序列./p>

逐字字符串文字包含一个@字符,后跟一个双引号字符,零个或多个字符以及一个结束的双引号字符.一个简单的例子是@"hello".在逐字字符串文字中,定界符之间的字符逐字解释,唯一的例外是引号-转义序列.特别是,简单的转义序列以及十六进制和Unicode转义序列不在逐字字符串文字中处理.逐字字符串文字可能跨越多行.

因此,当您将其与verbtaim字符串文字一起使用时;

  string firstString = @"使用\ n换行." ;;字符串secondString = @使用\ b作为退格键";返回firstString + secondString; 

结果将是

 使用\ n换行.使用\ b退格 

当您使用常规字符串文字时,例如;

  string firstString =使用\ n换行." ;;string secondString =使用\ b退格";返回firstString + secondString; 

结果将是

 使用换行.用于退格键 

因为 \ n 是换行符,而 \ b 是退格符.对于所有列表,请查看;

I am using C# to concatenate two strings with escapes sequences, that i want to skip so I'm using @ symbol before each string. It looks like this:

string firstString = @"Use \n for line break. ";
string secondString = @"Use \b for backspace";
return firstString + secondString;

The question is: Will that escapes sequences be skipped in the returned value?;

解决方案

Other answers are of course correct. For making it clear;

This is covered in section 2.4.4.5 of the C# specification:

2.4.4.5 String literals

C# supports two forms of string literals: regular string literals and verbatim string literals.

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

So when you use it with verbtaim string literal like;

string firstString = @"Use \n for line break. ";
string secondString = @"Use \b for backspace";
returns firstString + secondString; 

Result will be;

Use \n for line break. Use \b for backspace

When you use regular string literal like;

string firstString = "Use \n for line break. ";
string secondString = "Use \b for backspace";
returns firstString + secondString; 

Result will be;

Use
 for line break. Use for backspace

Because \n is new line escape sequence and \b is backspace escape sequence. For all list, take a look at;

这篇关于将两个字符串与转义序列连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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