×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

split character nearest to maximum line length
2

split character nearest to maximum line length

split character nearest to maximum line length

(OP)
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 --> vbscript

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.

RE: split character nearest to maximum line length

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

RE: split character nearest to maximum line length

(OP)
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!

RE: split character nearest to maximum line length

Er ... does that work reliably? I fed a comma seperated list to it and it gave some odd results. For example, feeding in

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.

with a maxlinewidth of 50 gives:

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


which looks wrong, and has lost several list items ...

RE: split character nearest to maximum line length

(OP)
I did notice something was a little off - & that would explain it. Anyway, corrected script below:

CODE --> vbscript

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 

RE: split character nearest to maximum line length

Still not quite correct - doesn't count the commas as part of the line length

RE: split character nearest to maximum 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

RE: split character nearest to maximum line length

That's better ... smile

(although still not quite right, as the new string starts with a vbCRLF ...)

RE: split character nearest to maximum line length

(OP)
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 --> vbscript

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 


RE: split character nearest to maximum line length

Here's a Regular Expression variant:

CODE

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 

RE: split character nearest to maximum line length

(OP)
Thanks that's quite a neat little function (just need to remove the '$' from the last Trim$).

RE: split character nearest to maximum line length

Yep, sorry. Extracted from a VB test rather than VBScript

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close