0

I'm facing a odd problem were C# is unable to split a string for new lines. I tried many combinations like use only Split.('\n') but all lead to return the whole string unsplited on first position of the array so lines[0] is the same as the input string to be splited, that never happen before with other strings i had to parse.

Image bellow:

enter image description here

String:

Don't remove the following keywords! These keywords are used in the "compatible printer" condition of the print and filament profiles to link the particular print and filament profiles to this printer profile.\nPRINTER_VENDOR_PRUSA3D\nPRINTER_MODEL_SL1\nPRINTER_VENDOR_EPAX\nPRINTER_MODEL_X1\n\nSTART_CUSTOM_VALUES\nFLIP_XY\nLayerOffTime_0\nBottomLightOffDelay_2\nBottomLiftHeight_5\nLiftHeight_5.5\nBottomLiftSpeed_40.2\nLiftSpeed_60\nRetractSpeed_150\nBottomLightPWM_255\nLightPWM_255\nAntiAliasing_4 ; Use 0 or 1 for disable AntiAliasing with "printer gamma correction" set to 0, otherwise use multiples of 2 and "gamma correction" set to 1 for enable\nEND_CUSTOM_VALUES

Code:

var lines = previousString.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);

Output: An array of lenght = 1 producing lines[0] == previousString

  • Please, post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), and don't post [images of code, but the code itself](https://idownvotedbecau.se/imageofcode). – Magnetron Jul 17 '20 at 16:13
  • https://stackoverflow.com/questions/62889586/split-string-by-new-lines-multiple-lines-with-different-spacing/62889644#62889644 – Legacy Code Jul 17 '20 at 16:13
  • 5
    My hunch is that the text you are splitting doesn't actually have newlines in it: I suspect those are really the two characters: backslash & en. Try splitting on "\\n" to confirm. – Jamie F Jul 17 '20 at 16:16
  • Magnetron i posted a image because it shows the debugger and the proof of the problem. I think 1 line code isn't that dificult to copy from image, all my code is doing is a string.split, nothing more, but i will post the string as text here to be easier to copy it and see. Edited now – Tiago Conceição Jul 17 '20 at 16:17
  • @Jamie F you are right double back slash (\\n) solved the problem, this is odd since i read the text from a file, need to back check it. Thanks you can post it as a anwser so i can mark it as solved – Tiago Conceição Jul 17 '20 at 16:24

3 Answers3

4
string[] lines = theText.Split(
    new[] { Environment.NewLine },
    StringSplitOptions.None
);

edit:

string[] lines = theText.Split(
    new[] { "\r\n", "\r", "\n" },
    StringSplitOptions.None
);

working fiddle: https://dotnetfiddle.net/HNY8a6

See: this SO post

jawsofdoom
  • 307
  • 2
  • 10
  • 3
    I think you didn't look at my code by your anwser... I tried that and produce same effect, in fact that cover less possibilities than the aproach i'm using { "\r\n", "\r", "\n" } – Tiago Conceição Jul 17 '20 at 16:13
1

Sometimes when you see a \n on screen it really is a backslash (ASCII 92 and an en(ASCII 110) not a placeholder/escape sequence for new line (ASCII 10) A big hint for that here is that text boxes will usually not display newlines with escape codes but will put in actual new lines.

To split on \n use the string "\\n" which represents a string of two characters: the two backslashes produce a single character ASCII 92 = '' in the string and then a lowercase n.

Alternately you could use @"\n". The @ sign tells C# not to use escape codes in the quoted string.

Jamie F
  • 23,189
  • 5
  • 61
  • 77
0

I'm not quite sure why you are using the Printer methods but I hope you don't require them.

string test = "Hello \nTest \n123"; //Create Test String
string[] seperated = test.Split('\n'); //Splite String by '\n'
        
for(int i = 0; i < seperated.Length; i++){ //Output substrings
    Console.WriteLine(seperated[i]);    
}

Output:

Hello 
Test
123

I hope this solution works for you!

Edit: Added \r\n and \r support If you also need to split strings by '\r' or '\r\n' then this code is the one to go with.

string test = "Hello \r\nTest \n123 \rEnd"; //Create Test String
test = test.Replace("\r\n","\n");
test = test.Replace("\r","\n");
string[] seperated = test.Split('\n'); //Splite String by '\n'
    
for(int i = 0; i < seperated.Length; i++){ //Output substrings
    Console.WriteLine(seperated[i]);    
}

Output:

Hello 
Test 
123 
End

Edit2: Hopefully Solution So you are saying that

\nPRINTER_VENDOR_PRUSA3D\nPRINTER_MODEL_SL1\nPRINTER_VENDOR_EPAX\nPRINTER_MODEL_X1\n\nSTART_CUSTOM_VALUES\nFLIP_XY\nLayerOffTime_0\nBottomLightOffDelay_2\nBottomLiftHeight_5\nLiftHeight_5.5\nBottomLiftSpeed_40.2\nLiftSpeed_60\nRetractSpeed_150\nBottomLightPWM_255\nLightPWM_255\nAntiAliasing_4 ; Use 0 or 1 for disable AntiAliasing with "printer gamma correction" set to 0, otherwise use multiples of 2 and "gamma correction" set to 1 for enable\nEND_CUSTOM_VALUES

is the string then the problem might be that this string contains some " which will interfere with the .Split method

If you're able to input the string manually you should replace a simple " with a "

Paul595
  • 21
  • 5