Determine a space character in a string

Posted on 2007-10-01
Last Modified: 2013-11-26
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.  
Question by:mlupino
    LVL 14

    Expert Comment

    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
    LVL 34

    Accepted Solution

    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.

    LVL 2

    Author Comment

    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.
    LVL 2

    Author Comment

    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.
    LVL 34

    Expert Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now