Best Way to Split String into Lines

Best way to split string into lines

  • If it looks ugly, just remove the unnecessary ToCharArray call.

  • If you want to split by either \n or \r, you've got two options:

    • Use an array literal – but this will give you empty lines for Windows-style line endings \r\n:

      var result = text.Split(new [] { '\r', '\n' });
    • Use a regular expression, as indicated by Bart:

      var result = Regex.Split(text, "\r\n|\r|\n");
  • If you want to preserve empty lines, why do you explicitly tell C# to throw them away? (StringSplitOptions parameter) – use StringSplitOptions.None instead.

How to split string into a lines in a particular sequence

The RichTextBox has a Lines property that returns an array of the lines. I used a StringBuilder to build the string for RichTextBox2. The For loop increments by 4 instead of the default 1.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ParseRTB()
End Sub
Private Sub ParseRTB()
Dim lines = RichTextBox1.Lines
Dim sb As New StringBuilder
For i = 0 To lines.Count - 4 Step 4
sb.AppendLine($"{lines(i)};{lines(i + 1)};{lines(i + 2)};{lines(i + 3)}")
Next
RichTextBox2.Text = sb.ToString
End Sub

Easiest way to split a string on newlines in .NET?

To split on a string you need to use the overload that takes an array of strings:

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

Edit:

If you want to handle different types of line breaks in a text, you can use the ability to match more than one string. This will correctly split on either type of line break, and preserve empty lines and spacing in the text:

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

How do I split a multi-line string into multiple lines?

inputString.splitlines()

Will give you a list with each item, the splitlines() method is designed to split each line into a list element.

Split Java String by New Line

This should cover you:

String lines[] = string.split("\\r?\\n");

There's only really two newlines (UNIX and Windows) that you need to worry about.

splitting string by spaces and new lines

I hope i understood your question right but if you're looking to extract the number and return them in the specific format you could go like this :

    // Assuming the String would be like a repetition of [word][space][number][b][\n]
String testString = "xyz 100b\nabc 200b\ndef 400b";

// Split by both endOfLine and space
String[] pieces = testString.split("\n| ");

String answer = "";

// pair index should contain the word, impair is the integer and 'b' letter
for (int i = 0; i < pieces.length; i++) {
if(i % 2 != 0 ) {
answer = answer + "answer " + ((i/2)+1) + ": " + pieces[i] + "\n";
}
}
System.out.println(answer);

This is the value of answer after execution :

answer 1: 100b
answer 2: 200b
answer 3: 400b

Best way to split string into lines with maximum length, without breaking words

How about this as a solution:

IEnumerable<string> SplitToLines(string stringToSplit, int maximumLineLength)
{
var words = stringToSplit.Split(' ').Concat(new [] { "" });
return
words
.Skip(1)
.Aggregate(
words.Take(1).ToList(),
(a, w) =>
{
var last = a.Last();
while (last.Length > maximumLineLength)
{
a[a.Count() - 1] = last.Substring(0, maximumLineLength);
last = last.Substring(maximumLineLength);
a.Add(last);
}
var test = last + " " + w;
if (test.Length > maximumLineLength)
{
a.Add(w);
}
else
{
a[a.Count() - 1] = test;
}
return a;
});
}

I reworked this as prefer this:

IEnumerable<string> SplitToLines(string stringToSplit, int maximumLineLength)
{
var words = stringToSplit.Split(' ');
var line = words.First();
foreach (var word in words.Skip(1))
{
var test = $"{line} {word}";
if (test.Length > maximumLineLength)
{
yield return line;
line = word;
}
else
{
line = test;
}
}
yield return line;
}


Related Topics



Leave a reply



Submit