Determine a space character in a string

I would like to write a statement that does the following but I am having alot of trouble so all the help is appreciated.

The statement will:
take a string of (x character's) size to be determined by running program from a variable (such as Description)
break it down into 40 character segments
Reverse from 40 to a space ??? I am unsure on how to do that.
Print the number of characters from 1 to the character with a space.
Be contained in a loop so that I can pick up from the space and pick up another 40 characters.

Any help is greatly appreciated.  
Who is Participating?
Here's a function from one of my VB.NET 2003 utility modules

    Private Function SetLineLength(ByVal intext As String, ByVal LineLength As Integer, Optional ByVal PreserveBreaks As Boolean = True, Optional ByVal LineTerminator As String = vbCrLf) As String
        Dim result As String = ""
        Dim oldLines As String()
        If PreserveBreaks Then
            oldLines = Regex.Split(intext, LineTerminator)
            ReDim oldLines(0)
            oldLines(0) = Regex.Replace(intext, LineTerminator, " ")
        End If
        For i As Integer = 0 To oldLines.Length - 1
            Dim n As String = ""
            Dim t As String = ""
            Dim s As String = oldLines(i)
                If s.Length > LineLength Then
                    t = s.Substring(0, LineLength)
                    Dim p As Integer = t.LastIndexOf(" ")
                    n &= s.Substring(0, p).Trim & LineTerminator
                    s = s.Substring(p).Trim
                    n &= s.Trim
                    Exit Do
                End If
            If result <> "" Then
                result &= LineTerminator
            End If
            result &= n
        Return result
    End Function

It needs

Imports System.Text.RegularExpressions

at the start of the file.

You can use instr or string.indexof to get the position of the space

if you just want to split the string at the spaces then you can use string.split function
mlupinoAuthor Commented:
Thank you Roger.

its been awhile since I programmed. I wanted to see if I could handle this task internally at my job, I spent all day trying out different types of loops.... (Do, Do While) and For, Next and wasn't making a dent.
mlupinoAuthor Commented:
I am having some trouble validating the code is working OK. It is probably me. I wanted to test it under a different condition, by adding a textbox1.text output to each broken string. I think seeing that may help me. I created six different textboxs from textbox1.text to textbox6.text to represent a maximum of 240 characters entered in as input. Would you be able to show me how to output the result of the split strings to text boxes. Any help would be appreciated. If you'd like i can open another question on the topic so you can get additional points.
The simplest way to test is to use a single TextBox with its .MultiLine property set to True and then use code like.

        <myTextBox>.Text = SetLineLength(<TestText>, 40)

where myTextBox is the name of your textbox and TestText is the string you want breaking up.

If you want to put each line in a spearate textbox it becomes more complicated.  Basically you need to split up the string that is returned by the function

        Dim newlines() As String = Regex.Split(SetLineLength(<TestText>, 40), vbCrLf)

and then put each element of that newline array into one of the textboxes

        TextBox0.Text = newlines(0)
        TextBox1.Text = newlines(1)

That's easy enough, hard-coding.  But the complications come in (a) that in real life you won't know how many elements there will be in the array and (b) that there is no direct method of coding the equivalence

        TextBox? = newlines(?)

where ? represents an index number.  If you want to go down that latter line I think a new question would be appropriate as it's really a different topic from that of splitting up the string.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.