如何使用反斜线分割 [英] How to split using a back slash

查看:80
本文介绍了如何使用反斜线分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用'\'分割字符串,但是不允许,编译器以常数表示换行符。有没有办法使用反斜杠进行拆分?

I want to split a string using the '\', however its not allowed, the compiler says newline in constant. Is there a way to split using back slash?

//For example...
String[] breakApart = sentence.Split('\'); //this gives an error. 


推荐答案

尝试使用转义字符 '\\'而不是'\'

Try using the escaped character '\\' instead of '\':

String[] breakApart = sentence.Split('\\');

在C#中使用反斜杠 \ 作为引号和撇号等特殊字符的转义字符。因此,当您尝试用撇号包裹反斜杠时,反斜杠与最后的撇号一起被解释为转义的撇号。

The backslash \ in C# is used as an escape character for special characters like quotes and apostrophes. So when you are trying to wrap the backslash with apostrophes, the backslash together with the final apostrophe is being interpreted as an escaped apostrophe.

此处是C#中可用的字符转义列表。

Here is a list of character escapes available in C#.

此处是C#中的字符文字的MSDN文档。

Here is MSDN documentation of character literals in C#.

这篇关于如何使用反斜线分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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