edgarmtze
edgarmtze

Reputation: 25048

split multiline textbox text with new line depending on string length .NET

I have a multiline textbox and I want to make an array of strings, However I would like to concatenate a new line when string has more than 60 chars.

So supposing I have a text:

A geologic period is one of several subdivisions of geologic time enabling cross-referencing of rocks and geologic events from place to place. These periods form elements of a hierarchy of divisions into which geologists have split the earth's history.

be converted to:

A geologic period is one of several subdivisions of geologic \n
time enabling cross-referencing of rocks and geologic events \n
from place to place. These periods form elements of a        \n
hierarchy of divisions into which geologists have split the  \n
earth's history.

So to do so I was plannig to use

array_strings = myMultilineText.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)

and then loop through each string in array like

 For index = 0 To array_strings.GetUpperBound(0)
    If array_strings(index).Length < 60 Then
        MessageBox.Show(array_strings(index))
    Else
       'add new line...
    End If
 Next

Is there a better way to do this?

Upvotes: 1

Views: 1979

Answers (1)

Arin Ghazarian
Arin Ghazarian

Reputation: 5305

If you want your text to be wrapped in a TextBox you can just set the "WordWrap" property to True. But in case you want an algorithm you can use this code in c# (you can convert it to VB if you want, it's so simple)

c# Code:

        string longString = "Your long string goes here...";

        int chunkSize = 60;
        int chunks = longString.Length / chunkSize;
        int remaining = longString.Length % chunkSize;
        StringBuilder longStringBuilder = new StringBuilder();
        int index;
        for (index = 0; index < chunks * chunkSize; index += chunkSize)
        {
            longStringBuilder.Append(longString.Substring(index, chunkSize));
            longStringBuilder.Append(Environment.NewLine);
        }
        if (remaining != 0)
        {
            longStringBuilder.Append(longString.Substring(index, remaining));
        }

        string result = longStringBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray());

VB Code (Converted via developerfusion Conversion Tool):

Dim longString As String = "Your long string goes here..."

Dim chunkSize As Integer = 60
Dim chunks As Integer = longString.Length \ chunkSize
Dim remaining As Integer = longString.Length Mod chunkSize
Dim longStringBuilder As New StringBuilder()
Dim index As Integer
index = 0
While index < chunks * chunkSize
    longStringBuilder.Append(longString.Substring(index, chunkSize))
    longStringBuilder.Append(Environment.NewLine)
    index += chunkSize
End While
If remaining <> 0 Then
    longStringBuilder.Append(longString.Substring(index, remaining))
End If

Dim result As String = longStringBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray())

Upvotes: 4

Related Questions