Solved

# Determine a space character in a string

Posted on 2007-10-01
3,413 Views
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
Question by:mlupino

LVL 14

Expert Comment

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

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)
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

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.
0

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.
0

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)
'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

### 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 (http://www.ecb.europa.eu/stats/exch…
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.