[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3432
  • Last Modified:

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.  
0
mlupino
Asked:
mlupino
  • 2
  • 2
1 Solution
 
shahprabalCommented:
You can use instr or string.indexof to get the position of the space

http://msdn2.microsoft.com/en-us/library/d93tkzah.aspx

if you just want to split the string at the spaces then you can use string.split function

http://msdn2.microsoft.com/en-us/library/system.string.split(VS.71).aspx
0
 
SanclerCommented:
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)
        Else
            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)
            Do
                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
                Else
                    n &= s.Trim
                    Exit Do
                End If
            Loop
            If result <> "" Then
                result &= LineTerminator
            End If
            result &= n
        Next
        Return result
    End Function

It needs

Imports System.Text.RegularExpressions

at the start of the file.

Roger
0
 
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.
0
 
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.
0
 
SanclerCommented:
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)
        'etc

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.

Roger
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now