使用反斜杠分割字符串 [英] Split string using backslash

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

问题描述

我想使用反斜杠('\')分割字符串.但是,这是不允许的-编译器说"newline in constant".有没有办法使用反斜杠进行分割?

I want to split a string using the backslash ('\'). However, it's not allowed - the compiler says "newline in constant". Is there a way to split using backslash?

//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#.

此处是Microsoft的C#字符文字文档.

Here is Microsoft's documentation for character literals in C#.

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

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