2010-09-17 4 views

Répondre

0

En supposant que vous êtes des mots de rupture sur un espace.

startIndex = 0 
index = 1 
do while len(inputString) - startIndex > desiredLength 
    tmp = mid(inputString, 0, desiredLength) 
    outputStrings[index] = left(tmp, instrrev(tmp, " ")-1) 
    startIndex = startIndex + len(outputStrings[index]) + 1 
    index = index + 1 
loop 
0

@Caveatrob: en supposant également vous cassez sur les espaces:

<% 
Option Explicit 

Function TrimIt(sInput, iLength) 
    Dim aWords, iCounter, sOutput, sTmp 

    sOutput = sInput 

    If InStr(sInput, " ") > 0 Then 
     aWords = Split(sInput, " ") 
     For iCounter = 0 To UBound(aWords) 
      If Len(aWords(iCounter) & " ") + Len(sTmp) <= iLength Then 
       sTmp = sTmp & " " & aWords(iCounter) 
      Else 
       Exit For 
      End If 
     Next 
     sOutput = sTmp 
    End If 

    TrimIt = sOutput 
End Function 

Response.Write TrimIt("This works slightly better", 11) 
%> 
Questions connexes