Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split character nearest to maximum line length 2

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
GB
Hi,

I have a list of comma separated items, but only have a limited line length (aprox 50 chars) to display them. If I split them at the comma, I'll end up with a long list of items. What I'd like to to is fit as many as I can on each line. In essence I'd like to loop through the string & replace the comma that's nearest to the maximum line length & replace it with a vbcrlf.

I've tried using a standard split & looping through the array, checking the length of each array item in turn & if that was less than the max length I then added it to a variable, but I'm not having much luck (the code below is an example of the sort of thing I was trying, I'm away from the script I had written so this is more pseuedo-type code). Something like:

Code:
if len(mylongstring) > maxlinelength Then
mylongstring_arr = split(mylongstring,",")
for x = 0 to ubound(mylongstring_arr)
If len(mylongstring_arr(x)+newstring) <= maxlinelength Then
newstring = newstring &","&mylongstring_arr(x)
Else
newstring = newstring &vbcrlf&mylongstring_arr(x)
End If
Next
End If

Any help would be appreciated.
 
You could build lines first and next append them to the output string:
Code:
if len(mylongstring) > maxlinelength Then
mylongstring_arr = split(mylongstring,",")
LineString = ""
for x = 0 to ubound(mylongstring_arr)
  If len(LineString & "," & mylongstring_arr(x)) <= maxlinelength Then
  LineString = LineString & "," & mylongstring_arr(x)
Else
  newstring = newstring & vbcrlf & LineString
  LineString = ""
End If
Next
If Len(LineString)>0 Then newstring = newstring & vbcrlf & LineString
End If


combo
 
That's great, thanks. I think I was looking at it from the wrong perspective - I spent far too long trying to get it to work before posting on here!
 
Er ... does that work reliably? I fed a comma seperated list to it and it gave some odd results. For example, feeding in

[tt]Now, is, the, winter, of, our, discontent, Made, glorious, summer, by, this, sun, of, York, And, all, the, clouds, that, lour'd, upon, our, house, In, the, deep, bosom, of, the, ocean, buried.[/tt]

with a maxlinewidth of 50 gives:

[tt],Now, is, the, winter, of, our, discontent, Made
, summer, by, this, sun, of, York, And, all, the
, that, lour'd, upon, our, house, In, the, deep
, of, the, ocean, buried.[/tt]

which looks wrong, and has lost several list items ...
 
I did notice something was a little off - & that would explain it. Anyway, corrected script below:

Code:
if len(mylongstring) > maxlinelength Then
	mylongstring_arr = split(mylongstring,",")
	LineString = ""
	for x = 0 to ubound(mylongstring_arr)
		If len(LineString & "," & mylongstring_arr(x)) <= maxlinelength Then
			If LineString <> "" Then
				LineString = LineString & "," & mylongstring_arr(x)
			Else
				LineString = mylongstring_arr(x)
			End If
		Else
			Linestring = Linestring &vbcrlf &mylongstring_arr(x)
			If newstring <> "" Then
				newstring = newstring &","& LineString
			else
				newstring = Linestring
			End If
			LineString = ""
		End If
	Next
	If Len(LineString)>0 Then 
		newstring = newstring & LineString
		newstring = replace(newstring,vbcrlf&" ",vbcrlf)
	End If
 
Still not quite correct - doesn't count the commas as part of the line length
 
This includes words linger than 50 characters too:
Code:
mylongstring = "Now, is, the, winter, of, our, discontent, Made, glorious, summer, by, this, sun, of, York, And, all, the, clouds, that, lour'd, upon, our, house, In, the, deep, bosom, of, the, ocean, buried."
maxlinelength = 50
If Len(mylongstring) > maxlinelength Then
    mylongstring_arr = Split(mylongstring, ",")
    LineString = ""
    For x = 0 To UBound(mylongstring_arr)
        If Len(LineString) = 0 Then
            LineString = LTrim(mylongstring_arr(x))
        ElseIf Len(LineString & "," & mylongstring_arr(x)) <= maxlinelength Then
            LineString = LineString & "," & mylongstring_arr(x)
        Else
            newstring = newstring & vbCrLf & LineString
            LineString = LTrim(mylongstring_arr(x))
        End If
    Next
    If Len(LineString) > 0 Then newstring = newstring & vbCrLf & LineString
End If

combo
 
That's better ... :)

(although still not quite right, as the new string starts with a vbCRLF ...)
 
Thanks. I hadn't really noticed the seperator length issue as my maximum line length wasn't constrained, so it sort of looking right was fine for what I needed. However I do both appreciate you both taking the time to look at this & provide a fully working solution.

I did correct the script for the leading vbcrlf :)

Code:
maxlinelength = 50
mylongstring = "Now, is, the, winter, of, our, discontent, Made, glorious, summer, by, this, sun, of, York, And, all, the, clouds, that, lour'd, upon, our, house, In, the, deep, bosom, of, the, ocean, buried."

if len(mylongstring) > maxlinelength Then
	mylongstring_arr = split(mylongstring,",")
	LineString = ""
	For x = 0 To UBound(mylongstring_arr)
		If Len(LineString) = 0 Then
			LineString = LTrim(mylongstring_arr(x))
		ElseIf Len(LineString & "," & mylongstring_arr(x)) <= maxlinelength Then
			LineString = LineString & "," & mylongstring_arr(x)
		Else
			If newstring <> "" Then
				newstring = newstring & vbCrLf & LineString
			Else
				newstring = LineString			
			End If
			LineString = LTrim(mylongstring_arr(x))
		End If
	Next
	If Len(LineString)>0 Then
		newstring = newstring &vbCrLf & LineString
	End If 
End If
wscript.echo newstring

 
Here's a Regular Expression variant:

Code:
[blue]Function Linebreak(strSource, linewidth, strSeperator) As String
    Dim myMatches
    Dim myMatch
    Dim wombat()
    With CreateObject("vbscript.regexp")
        .Global = True
        .Pattern = "(.|" & strSeperator & "){0," & linewidth & "}(" & strSeperator & " |$)"
        Set myMatches = .Execute(strSource)
    End With
    For Each myMatch In myMatches
        If Linebreak <> "" And Trim(myMatch) <> "" Then Linebreak = Linebreak & vbCrLf
        Linebreak = Linebreak & Trim$(myMatch)
    Next
End Function[/blue]
 
Thanks that's quite a neat little function (just need to remove the '$' from the last Trim$).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top