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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert carriage return every 80 characters 1

Status
Not open for further replies.

Niavlys

IS-IT--Management
Jun 3, 2002
197
CA
Hi,
I need to add a vbCrLf every 80 characters in a string. I can't figure it out. I don't know much about string functions.

Can someone help me?
Thanks in advance
 
Hi,

If str contains the string...
Code:
   s = Split(str, " ")
   WrapText = s(0)
   For i = 1 To UBound(s)
      If Len(WrapText) Mod 80 > Len(WrapText & " " & s(i)) Mod 80 Then
         WrapText = WrapText & vbCrLf & s(i)
      Else
         WrapText = WrapText & " " & s(i)
      End If
   Next
WrapText has the string with the vbCrLf characters

Skip,

[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue]
 
Here is the solution I came up with Skip. I think one of us didn't understand the question. I thaought the goal was to cause text that is >80 chars to wrap at 80.
Code:
Option Explicit
Dim strString

strString = String(80, "a") & String (80, "b") & String(80, "c")
WScript.Echo "BEFORE: " & VbCrLf & strString
strString = InsertReturns(strString, 80)
WScript.Echo "AFTER:" & VbCrLf & strString

Function InsertReturns(strTxt, nCount)
	Dim nInsert
	
	nInsert = nCount
	Do While nInsert < Len(strTxt)
		strTxt = Left(strTxt, nInsert) & VbCrLf &  Mid(strTxt, nInsert + 1)
		nInsert = nInsert + nCount + 2
	Loop
	InsertReturns = strTxt
End Function

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Exactly!

So do you arbitrarily split a word just because the 80th character is in the middle of a word? I should have made a function, my function wraps words within the 80 character limit. He can choose
Code:
Function WrapText(str As String)
   s = Split(str, " ")
   WrapText = s(0)
   For i = 1 To UBound(s)
      If Len(WrapText) Mod 80 > Len(WrapText & " " & s(i)) Mod 80 Then
         WrapText = WrapText & vbCrLf & s(i)
      Else
         WrapText = WrapText & " " & s(i)
      End If
   Next
End Function


Skip,

[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue]
 
It works perfectly Tom,

thanks for your help!
 
It would seem that neither solution is perfect. It is true that mine will put the return at the 80th char regardless of the word break. Your solution will not put a return at all if the string has no spaces.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
My solution would put the return in place of a space at

the last space in the string before the 80th character.

Skip,

[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue]
 
But what if the string contains no spaces? Or what if the string is 200 characters long with only one space at the 50th character?

Here is one that will handle long strings with no spaces, but will break on the spaces if they are there.
Code:
Option Explicit
Dim strString

strString = String(80, "a") & String (80, "b") & String(80, "c")
WScript.Echo "BEFORE: " & VbCrLf & strString
strString = InsertReturns(strString, 80)
WScript.Echo "AFTER:" & VbCrLf & strString
strString = "This is a very long line. I will get a lot of text with no line breaks" _
	& " It would seem that neither solution is perfect. It is true that mine will put" _
	& " the return at the 80th char regardless of the word break. Your solution will" _
	& " not put a return at all if the string has no spaces. My solution would put the" _
	& " return in place of a space at the last space in the string before the 80th character."
WScript.Echo "BEFORE: " & VbCrLf & strString
strString = InsertReturns(strString, 80)
WScript.Echo "AFTER:" & VbCrLf & strString

Function InsertReturns(strTxt, nCount)
	Dim nInsert
	Dim nBreak
	
	nInsert = nCount
	Do While nInsert < Len(strTxt)
		nBreak = InStrRev(strTxt, " ", nInsert)
		If nInsert - nBreak > nCount Then
			strTxt = Left(strTxt, nInsert) & VbCrLf &  Mid(strTxt, nInsert + 1)
		Else
			nInsert = nBreak
			strTxt = Left(strTxt, nInsert) & VbCrLf &  Mid(strTxt, nInsert + 1)
		End If
		nInsert = nInsert + nCount + 2
	Loop
	InsertReturns = strTxt
End Function

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top