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!

Splitting Words in two 1

Status
Not open for further replies.

janise

Technical User
May 25, 2003
161
US
Can I have you bell me out one more time, please?

I have a field on our table that we need to split the values into two.

The value is something like:

I am coming to see you today, Will you be at home?

We would like to split this like:

I am coming to see you today,
Will you be at home?

The field name is certcomment.

I tried to use the below code to splt but it isn't doing anything.

Code:
strDataField = rsData("certcomment")
intMidPoint = Round(Len(strDataField) / 2)
strDataField = Replace(strDataField, " ", "<br>", intMidPoint, 1)

Then the field is tagged here:

Code:
  <tr>
   <td colspan="4" align="center"><%=(strDataField)%></td>
  </tr>

Thanks in advance
 
arrWords = split(rsData("certcomment"),",")

response.write(arrWords(0) & "<br>" & arrWords(1))


Steve Davis
hey.you AT HaHaHa.com.au

Me? I can't even spell ASP!

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination
 
a simple split on position would give rise to the problem of splitting words across 2 lines. If this is not a issue then using left() and right() functions would work ok
like so;
Code:
function WrapOnMid(strIn)
dim intSplit
dim strLeft
dim strRight

intSplit = Round(Len(strIn) / 2)
strLeft = left(strIn,intSplit)
strRight = right(strIn,len(strIn) - intSplit)
WrapOnMid = strLeft & "<br>" & vbCrLf & strRight
end function

however if you want to maintain word integrity, a little more is needed;
this function will locate the closest space to the mid point and wrap on that
Code:
function WrapOnSpace(strIn)
dim intSplit
dim strLeft
dim strRight
dim intLeftSpace
dim intRightSpace

intSplit = round(len(strIn) / 2)
intLeftSpace = instrrev(strIn," ",intSplit)
intRightSpace = instr(intSplit,strIn," ")
if intLeftSpace > intRightSpace then
	intSplit = intRightSpace
elseif intRightSpace > intLeftSpace then
	intSplit = intLeftSpace
end if
	
strLeft = left(strIn,intSplit)
strRight = right(strIn,len(strIn) - intSplit)
WrapOnSpace = strLeft & "<br>" & vbCrLf & strRight
end function



Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
Yes Chris, yours is a more elaborate (read "well thought out") solution. I was assuming the split was just going to happen on the comma.

Steve Davis
hey.you AT HaHaHa.com.au

Me? I can't even spell ASP!

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination
 
Thanks so much Chris,

How do I call this function, please!

This didn't work for me:

Code:
  <tr>
   <td colspan="4" align="center">WrapOnSpace(<%=rsData("certcomment")%>)</td>
  </tr>
 
the function call needs to be inside code delimiters

Code:
<td colspan="4" align="center"><%response.write WrapOnSpace(rsData("certcomment"))%></td>

and of course the function code needs to be on the page or in an included file.


Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
This is perfect, Chris!
Thank you very much for your time and for sharing your expertise.
 
Hi there was looking for something similar
will this do the following

THIS IS A TEST

Put THIS
IS
A
TEST
Because i need something that would do
a limit of characters i.e i set it to 10
and get
THIS IS A
TEST

Cheers
 
Another one from the library.

Code:
function WordWrap(strIn, intWrapLen)
     dim strOut
     dim intLenStrIn
	 dim intCurrPos
     dim intLineStart
     dim intWrapPos

     intLenStrIn = Len(strIn)

     intCurrPos = 1
     intLineStart = 1
     
     do while intCurrPos < intLenStrIn
          if mid(strIn, intCurrPos, 1) = " " then
               intWrapPos = intCurrPos
          end if
          if intCurrPos = intLineStart + intWrapLen then
               strOut = strOut & trim(mid(strIn,intLineStart,intWrapPos - intLineStart + 1)) & "<br>" & vbCrLf

               intLineStart = intWrapPos + 1

               do while mid(strIn, intLineStart, 1) = " "
                    intLineStart = intLineStart + 1
               loop
          end if

          intCurrPos = intCurrPos + 1
     loop

     strOut = strOut & trim(mid(strIn,intLineStart)) & vbCrLf

     WordWrap = strOut
end function

I'm actually working on a site redesign where a lot of these code snippets and functions will be listed and explained.



Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
I put in
<%response.write WordWrap("test me for this again and again and now then so what",5)%>
and got
test
me
for
this
etc

i was hoping for test me for this etc

Thanks
Where is your site.
 
Oh i got it i put in
<%response.write WordWrap("test me for this again and again and now then so what",25)%>

and now get this

test me for this again
and again and now then so
what

This is great....But i also need amount of words across page and say 4 lines down also.

If greater than say 25 characters across and 4 lines then pop up message hey user you have put too much into a this box...

If you could help me do this...i would be greatly appreciated and you probably save me an headache
 
You'd just add a counter to determine the number of vbCrLfs that were added.

Add
Code:
Dim intLines
to the top, then where you're setting the default values for your other variables, add
Code:
intLines = 1
Then inside the innermost If...Then where the <br> is added in you'd add a line that said
Code:
intLines = intLines + 1
At the end of the function the variable intLines will contain the total number of lines in the string. At that point you can either immediately stop things and output an error or you can, for example, return a blank string, which you can check for in your code, a la
Code:
    If intLines > 4 Then
        WordWrap = ""
    Else
         WordWrap = strOut
    End If
That's the basic concept of a counter:

1. Initialize it

2. Increment (add to it) when a condition occurs

3. Check it or use it later
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top