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

Nested functions Replace, Mid, InStr 1

Status
Not open for further replies.

PSchubert

Technical User
Jun 6, 2006
50
0
0
AU
Hi all,

What I want: to force a line break after the first space after the 30th character.

What I have that doesn't work:

Code:
Dim strCondNotes

strCondNotes = rs("mfldConditionNotes")

Replace(strCondNotes,(Mid(strCondNotes,(InStr(30,strCondNotes," ")),1))," " & "<br>")

This is as close as I've gotten, but it puts a line break after every space.

What have I missed?

Thanks!
 
Replace replaces all occurences unless the count is specified. Try
Code:
Replace(strCondNotes,(Mid(strCondNotes,(InStr(30,strCondNotes," ")),1))," " & "<br>"[COLOR=red], 1, 1[/color])
 
Thanks for your reply, xwb. Your suggestion puts the line break at the first space, instead of at the next space after the 30th character, but I'll play around with it some more along the lines you've suggested.
 
WordWrap function from my code library
Variable names are pretty much self explanatory.

Code:
Function WordWrapX(strTextToBeWrapped, intMaxLineLength)
     Dim strWrappedText           ' Result storage
     Dim intLengthOfInput         ' Length of original
     Dim intCurrentPosition       ' Where we're at now
     Dim intCurrentLineStart      ' Where the current line starts
     Dim intPositionOfLastSpace   ' Last space we saw

     intLengthOfInput = Len(strTextToBeWrapped)

     intCurrentPosition = 1
     intCurrentLineStart = 1
     
     Do While intCurrentPosition < intLengthOfInput
          If Mid(strTextToBeWrapped, intCurrentPosition, 1) = " " Then
               intPositionOfLastSpace = intCurrentPosition
          End If
          If intCurrentPosition = intCurrentLineStart + intMaxLineLength Then
               strWrappedText = strWrappedText & Trim(Mid(strTextToBeWrapped,intcurrentLineStart,intPositionOfLastSpace - intCurrentLineStart + 1)) & "<br>" & vbCrLf

               intCurrentLineStart = intPositionOfLastSpace + 1

               Do While Mid(strTextToBeWrapped, intCurrentLineStart, 1) = " "
                    intCurrentLineStart = intCurrentLineStart + 1
               Loop
          End If

          intCurrentPosition = intCurrentPosition + 1
     Loop

     strWrappedText = strWrappedText & Trim(Mid(strTextToBeWrapped,intcurrentLineStart)) & vbCrLf

     WordWrap = strWrappedText
End Function


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks for the code, Chris. For some reason, I couldn't get it to work--it returned an empty string. But, further searching led me to this, which is working as needed:

Code:
<%
Function WordWrap(theString, wrapLength)

	Dim tempString,lenOfString,index
	Dim lastSpace,startOfLine

	lenOfString = Len(theString)
	index = 1
	startOfLine = 1

	While index < lenOfString
		If Mid(theString, index, 1) = " " Then
			lastSpace = index
		End If

		If index = startOfLine + wrapLength Then
			tempString = tempString & Trim(Mid(theString, startOfLine, lastSpace - startOfLine + 1)) & "<br>"
			startOfLine = lastSpace + 1

			While Mid(theString, startOfLine, 1) = " "
				startOfLine = startOfLine + 1
			Wend
		End If

		index = index + 1
	Wend

	tempString = tempString & Trim(Mid(theString,startOfLine)) & "<br>"

	WordWrap = tempString
End Function
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top