简体   繁体   English

在C#中使用\\字符分割字符串

[英]Split a string with the \ character in C#

I am trying to split this string in C#. 我正在尝试在C#中拆分此字符串。

"FiO2 at 0.40\n36°C-37°C\nFlow at 5 L/min"

I have tried using this code: 我尝试使用此代码:

string[] splitVapo = value.Split('\u005c');

and this 和这个

string[] splitVapo = value.Split('\\');

But it does not work. 但这行不通。 Any suggestions? 有什么建议么?

\\n is a single char literal though it looks like two characters in the source code \\n是单个char文字,尽管在源代码中看起来像两个字符

\\n is a New line escape sequence ( msdn ) \\n是换行符序列( msdn

so 所以

var value = "FiO2 at 0.40\n36°C-37°C\nFlow at 5 L/min"; 
string[] splitVapo = value.Split('\n'); 

works just fine and splitVapo.Lenght is 3 可以正常工作并且splitVapo.Lenght是3

it would have worked also if you had provided correct hex code for \\n 如果您为\\n提供了正确的十六进制代码,它也会起作用

string[] splitVapo = value.Split('\u000A');

You want to split everytime you see '\\n' , correct? 您想在每次看到'\\n'拆分,对吗? You can try this: 您可以尝试以下方法:

string str = "FiO2 at 0.40\n36°C-37°C\nFlow at 5 L/min";            
string[] result = str.Split('\n');
for (int i = 0; i < result.Length; ++i) {
    //do something with result[i] if needed
}

由于\\n将被解释为新行,并且您无法更改字符串,因此请在Environment.Newline本身上拆分:

string[] splitVapo = value.Split(Environment.NewLine.ToCharArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM