0

I am trying to split this string in 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?

3
  • 6
    \n is new line. there is no \ character there. \ is used for escaping. so \\ in string means there is \. Commented Dec 16, 2015 at 10:16
  • var value = "FiO2 at 0.40\n36°C-37°C\nFlow at 5 L/min"; string[] splitVapo = value.Split('\n'); works just fine; splitVapo.Lenght is 3 Commented Dec 16, 2015 at 10:21
  • that worked a treat ASh put it as an answer and be rewarded Commented Dec 16, 2015 at 10:24

3 Answers 3

4

\n is a single char literal though it looks like two characters in the source code

\n is a New line escape sequence (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

it would have worked also if you had provided correct hex code for \n

string[] splitVapo = value.Split('\u000A');
Sign up to request clarification or add additional context in comments.

Comments

1

You want to split everytime you see '\n', correct? 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
}

2 Comments

Why a new char array of \n when Split already takes a character and you only use one?
@Bauss You are right. Sorry, that was simply my typing habit.
0

Since \n is going to be interpreted as a new line and you cannot change the string, split on Environment.Newline itself:

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

1 Comment

Split already takes a char, so converting it to an array of chars is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.