przemyslawklys
przemyslawklys
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
it completly ignores it
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
if string starts with new line and and ends with new line
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
ye, actually its' missing both
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
it's missing something, either the first or the last elements
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
it's close, but not the same
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
that's the idea... because then for every empty line in list i will add a Break() to word document
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
That's the best that I can do
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
List<string> Convert(string text) {
string[] splitStrings = { Environment.NewLine, "\r\n", "\n" };
string[] textSplit = text.Split(splitStrings, StringSplitOptions.RemoveEmptyEntries);
var list = new List<string>();
for (int i = 0; i < textSplit.Length; i++) {
// check if there's new line at the beginning of the text
// if there is add empty string to the list
if (i == 0 && text.StartsWith(Environment.NewLine)) {
list.Add("");
} else if (i == 0 && text.StartsWith("\r\n")) {
list.Add("");
} else if (i == 0 && text.StartsWith("\n")) {
list.Add("");
}
// add splitted text to the list
list.Add(textSplit[i]);

if (i < textSplit.Length - 1) {
// for every element in the list except the last element add empty string to the list
list.Add("");
} else {
// check if there's new line at the end of the text
// if there is add an empty string to the list
if (text.EndsWith(Environment.NewLine)) {
list.Add("");
} else if (text.EndsWith("\r\n")) {
list.Add("");
} else if (text.EndsWith("\n")) {
list.Add("");
}
}
}
return list;
}
List<string> Convert(string text) {
string[] splitStrings = { Environment.NewLine, "\r\n", "\n" };
string[] textSplit = text.Split(splitStrings, StringSplitOptions.RemoveEmptyEntries);
var list = new List<string>();
for (int i = 0; i < textSplit.Length; i++) {
// check if there's new line at the beginning of the text
// if there is add empty string to the list
if (i == 0 && text.StartsWith(Environment.NewLine)) {
list.Add("");
} else if (i == 0 && text.StartsWith("\r\n")) {
list.Add("");
} else if (i == 0 && text.StartsWith("\n")) {
list.Add("");
}
// add splitted text to the list
list.Add(textSplit[i]);

if (i < textSplit.Length - 1) {
// for every element in the list except the last element add empty string to the list
list.Add("");
} else {
// check if there's new line at the end of the text
// if there is add an empty string to the list
if (text.EndsWith(Environment.NewLine)) {
list.Add("");
} else if (text.EndsWith("\r\n")) {
list.Add("");
} else if (text.EndsWith("\n")) {
list.Add("");
}
}
}
return list;
}
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
i don't understand what I am doing most of the time
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
but I am pretty noobish
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
i would love to have this output from your string using something simpler
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
which is exactly what I need
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
on your string
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
Thats' what I get when i run my code
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
that seems to work, but seems a bit overkill 😉
87 replies
CC#
Created by przemyslawklys on 3/11/2023 in #help
❔ Split on new line preserving empty line
using System.Collections.Generic;

List<string> Convert(string text) {
string[] splitStrings = { Environment.NewLine, "\r\n", "\n" };
string[] textSplit = text.Split(splitStrings, StringSplitOptions.RemoveEmptyEntries);
var list = new List<string>();
for (int i = 0; i < textSplit.Length; i++) {
if (i == 0 && text.StartsWith(Environment.NewLine)) {
list.Add("");
} else if (i == 0 && text.StartsWith("\r\n")) {
list.Add("");
} else if (i == 0 && text.StartsWith("\n")) {
list.Add("");
}

list.Add(textSplit[i]);

if (i < textSplit.Length - 1) {
list.Add("");
} else {
if (text.EndsWith(Environment.NewLine)) {
list.Add("");
} else if (text.EndsWith("\r\n")) {
list.Add("");
} else if (text.EndsWith("\n")) {
list.Add("");
}
}
}
return list;
}

Console.WriteLine("----");
var test1 = Convert("First line\r\nAnd more \r\n in new line\r\n");
foreach (var item in test1) {
Console.WriteLine(item);
}
Console.WriteLine("----");
var test2 = Convert("First line\r\nAnd more \r\n in new line");
foreach (var item in test2) {
Console.WriteLine(item);
}
Console.WriteLine("----");
var test3 = Convert("First line\r\nAnd more \n in new line");
foreach (var item in test3) {
Console.WriteLine(item);
}
Console.WriteLine("----");
var test4 = Convert("\nFirst line\r\nAnd more \r\n in new line\r\n");
foreach (var item in test4) {
Console.WriteLine(item);
}
Console.WriteLine("----");
var test5 = Convert("\nFirst line\r\nAnd more " + Environment.NewLine + "in new line\r\n");
foreach (var item in test5) {
Console.WriteLine(item);
}
Console.WriteLine("----");
using System.Collections.Generic;

List<string> Convert(string text) {
string[] splitStrings = { Environment.NewLine, "\r\n", "\n" };
string[] textSplit = text.Split(splitStrings, StringSplitOptions.RemoveEmptyEntries);
var list = new List<string>();
for (int i = 0; i < textSplit.Length; i++) {
if (i == 0 && text.StartsWith(Environment.NewLine)) {
list.Add("");
} else if (i == 0 && text.StartsWith("\r\n")) {
list.Add("");
} else if (i == 0 && text.StartsWith("\n")) {
list.Add("");
}

list.Add(textSplit[i]);

if (i < textSplit.Length - 1) {
list.Add("");
} else {
if (text.EndsWith(Environment.NewLine)) {
list.Add("");
} else if (text.EndsWith("\r\n")) {
list.Add("");
} else if (text.EndsWith("\n")) {
list.Add("");
}
}
}
return list;
}

Console.WriteLine("----");
var test1 = Convert("First line\r\nAnd more \r\n in new line\r\n");
foreach (var item in test1) {
Console.WriteLine(item);
}
Console.WriteLine("----");
var test2 = Convert("First line\r\nAnd more \r\n in new line");
foreach (var item in test2) {
Console.WriteLine(item);
}
Console.WriteLine("----");
var test3 = Convert("First line\r\nAnd more \n in new line");
foreach (var item in test3) {
Console.WriteLine(item);
}
Console.WriteLine("----");
var test4 = Convert("\nFirst line\r\nAnd more \r\n in new line\r\n");
foreach (var item in test4) {
Console.WriteLine(item);
}
Console.WriteLine("----");
var test5 = Convert("\nFirst line\r\nAnd more " + Environment.NewLine + "in new line\r\n");
foreach (var item in test5) {
Console.WriteLine(item);
}
Console.WriteLine("----");
87 replies