❔ Split on new line preserving empty line
So I'm trying to create a method that given a string with
Environment.NewLine
or \r\n
or \r
or \n
converts it to an array while preserving the new lines in form of an empty line.
While testing things I am having hard time understanding why there is difference between first 2 lines vs what is in 3rd line. The first 2 lines when given a string such as "First line\r\nAnd more in new line"
split into array of 2 strings, while the output of textArray
splits into 3, with an empty line.
In the end I want to add to my C# library that can create Word Documents ability for people to be able to provide string with new lines of different kind and that would be treated in proper manner. But for some reason Split on newLineArray
delivers no empty lines, and the only time I can get it to deliver empty lines is when using Environmnet.NewLine.ToArray()
What I am missing?43 Replies
Using
Environmnet.NewLine.ToArray()
means you're splitting on \r
and then also on \n
.
So if the line is "blah\r\n"
then you get "blah", "".
Same as if you have "blah\ra\n"
then you get "blah", "a"you get
"blah"
, ""
, and ""
Not sure how 'implementation defined' this is, but this might be what you want:
.Split(new[]{"\r\n", "\r", "\n"}, StringSplitOptions.None);
importantly "\r\n"
is the first split string, so that's always checked first - at least in my local experiment.splitting on just
\r
doesn't really make sense i think
you can have a \r
without a new lineThat's now what I am seeing. It split just once and properly shows empty line in place of newline for both \r\n and explicit. My problem for the first question is - that even tho StringSplitOptions.None is supposed to act the same, I get 2 different results where while the split works on
text.Split(newLineArray, StringSplitOptions.None);
it's actually not preserving the empty entryThis doesn't seem to work for me. I mean the Split itself does work, the prbolem is the result is not "blah", "", "something else" but it's "blah", "something else". Only time "blah", "", "something else" is reteined if I use
text.Split(Environment.NewLine.ToArray(), StringSplitOptions.None);
I have no clue whyThe breakpoint never gets hit if I define text.Split suggested way
it hits just fine when using single .ToArray() approach
I guess the difference comes from
var test = Environment.NewLine.ToArray();
because what it shows now is that test is actually char[], that would mean char[] splits differently then string[]It seems like you're expecting split to keep new lines or convert them. It just doesn't
It doesn't 'show empty line in place of new line'
first 3 console write
gives 3,2,2
which means char is treated differently
and preserves new line on which we actually Split
when a string[] is passed
the empty line is not preserved
Your just doing different things
They're not treated differently
You give different input, you get different answer
i've edited the example above
showing same string, with split using 3 different, but very similar ways
first result gives 3, second gives 2, third gives 2
to my noob ass it looks the same 😉
when it comes to input,
This again
hrmms
It's all irrelevant. Split will not convert the split thing into something else... The split thing gets removed
ok, so what would the proper way to get this done
I want to convert a string into array, and retain new lines in a form of empty strings
Split on everything you want to split on. Then you have all your lines.
Then you can add in an empty line after every real line if you really want to
But how do I know where it is? thats' my problem
Because that's by definition what your array entries are
The things between the split tokens
Or use regex replace
To give a single token, then split on that token
🤯
Btw, what you're trying to do, I wouldn't suggest anyway.
After you're done, you don't know if an empty line is really an empty line, or if it's a 'new line'
how so?
It's for displaying in Word. User is only able to provide a string, so it has to be "explicit" in what they provide
i don't see how someone could feed empty line for my conversion to have a problem
but maybe i don't see something
so i wrote this, but it's not really working properly if it ends with new line
Because any string can be empty
But I would not split on empty string, so I would treat it without spliting and without adding a break
Wdym 'you wouldn't split'?
Split on an empty string is going to give an array of size 1 containing an empty string
If you don't want empty entries, which is what you're complaining about with the case where it ends with a new line, then use the option that removes empty entries
this seems to work fine
just seems a bit ugly
but not if the first element is new line
that seems to work, but seems a bit overkill 😉
This code honestly makes no sense. If you split on newline characters, no token in the resulting set will start with a newline character.
mtreit#6470
REPL Result: Success
Console Output
Compile: 770.253ms | Execution: 104.113ms | React with ❌ to remove this embed.
Thats' what I get when i run my code
on your string
which is exactly what I need
i would love to have this output from your string using something simpler
but I am pretty noobish
i don't understand what I am doing most of the time
That's the best that I can do
So, what, the result should just be a list of strings where every other item is an empty string...?
that's the idea... because then for every empty line in list i will add a Break() to word document
Or something
it's close, but not the same
it's missing something, either the first or the last elements
ye, actually its' missing both
if string starts with new line and and ends with new line
it completly ignores it
https://github.com/EvotecIT/OfficeIMO/pull/127 - here's why I am doing this
GitHub
Add ability to use NewLines ("\r\n"/Environment.NewLine) in AddPara...
This PR adds the ability to create line breaks using \n, \r\n or Environment.NewLine
var test = document.AddParagraph("TestMe").AddText("\nFirst line\r\nAnd more " + Environment...
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.