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

breaking a string at a space

Status
Not open for further replies.

TheDash

MIS
Mar 25, 2004
171
US
Hi,

Can someone suggest a funtion to break a string at the first space after each 30 characters of the string (string can be any length) and insert a line break so that it fits into the column space of a table.

This is what I have where it breaks after each 30 chars however I want to break it at a space.

If Len(str) > 30 Then
If Len(str) < 62 Then
str= Left(str, 30) & "<br>" & Right(str, Len(str) - 30)
str= Left(str, 61) & "<br>" & Right(str, Len(str) - 61)

Thanks in advance.
 
So let me see if I understand. Given a string of any length you want to insert a vbCrLf in place of the last space before the 30th character. Is that correct?

[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]
 
Example string in the tabe column:

Offers maps and satellite images for complex or pinpointed regional searches

I want to break it as below so that the column width is small

Offers maps and satellite images
for complex or pinpointed regional
searches

It breaks at images because the first space after the first 30 chars occurs after "images". The space after the second 30 chars occurs after "regional"


 
See if this gets you started in the right direction. It doesn't look for a space to make the break, but you should be able to modify it to.




'====================================================================
'
' Function: ColumnFormat(arrItems, nColumnSize, strJustification
' Purpose: Takes an array then formats and returns a string where
' the items in the array create columns of the specified
' length justified in the specified manner.
' CAUTIONS: Any input string that is longer than the column size
' will be end truncated to the column size
'
'====================================================================
Function ColumnFormat(arrItems, nColumnSize, strJustification)
Dim strFront
Dim strBack
Dim strReturn
Dim i

strReturn = ""
'Make sure none of the items are bigger than column size
For i = LBound(arrItems) To UBound(arrItems)
If Len(arrItems(i)) > nColumnSize Then arrItems(i) = Left(arrItems(i), nColumnSize)
Select Case UCase(strJustification)
Case "L"
strReturn = strReturn & arrItems(i) & Space(nColumnSize - Len(arrItems(i)))
Case "R"
strReturn = strReturn & Space(nColumnSize - Len(arrItems(i))) & arrItems(i)
Case "C"
strFront = Space(Int((nColumnSize-Len(arrItems(i)))/2))
strBack = Space(Int((nColumnSize-Len(arrItems(i)))/2) + (nColumnSize-Len(arrItems(i))) Mod 2)
strReturn = strReturn & strFront & arrItems(i) & strBack
End Select
Next
ColumnFormat = strReturn
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]
 
I wonder if there is a simple way to break a string. Your example is taking an array as parameter.
 
Ya, I forget what exactly I wrote it for. I think it was for cleaning up a text file after the fact so you would open a text file and use Split(oFSO.OPentextFile("PATHTOFILE").ReadAll(), vbCrLf) to generate an array that was passed to this function. It was just some old code that I had laying around. Not something I wrote specifically for your issue.

[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]
 
Do you want to break it at a maximum of 30 characters, but move backwards through the string until the first space (so you don't break a word)?
Code:
If Len(strOld) > 30 And Len(strOld) < 62 Then
  strL = Left(strOld, InStrRev(strOld, " ", 30))
  strR = Right(strOld, Len(strOld) - Len(strL))
  strNew = strL & "<br/> & strR
End If

With this code you could have a line of 20 with a second line of 42. If you're looking to always break at 30 characters (moving backwards to the first space) then you should be able to build a loop from this to handle that.
 
Not fully tested...but another thing you might play with....

Code:
Option Explicit

Dim strTemp : strTemp = "Offers maps and satellite images for complex or pinpointed regional searches"

Dim RegEx : Set RegEx = New RegExp
RegEx.Pattern = ".{1,33}(\s|$)"
RegEx.IgnoreCase = True
RegEx.Global = True
Dim colMatches : Set colMatches = RegEx.Execute(strTemp)
Dim objMatch
For Each objMatch In colMatches
	WScript.Echo objMatch.Value
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top