Asp classic vbscript返回整词字符串<=指定限制

Asp classic vbscript返回整词字符串<=指定限制,asp-classic,vbscript,Asp Classic,Vbscript,我想在vbScript(经典ASP)中,在不剪切单词的情况下将字符串解析为指定长度。最后一个字符串可能小于指定的长度(以避免剪切单词),但不应超过该长度。假设您在空格中打断单词 startIndex = 0 index = 1 do while len(inputString) - startIndex > desiredLength tmp = mid(inputString, 0, desiredLength) outputStrings[index] = left(t

我想在vbScript(经典ASP)中,在不剪切单词的情况下将字符串解析为指定长度。最后一个字符串可能小于指定的长度(以避免剪切单词),但不应超过该长度。

假设您在空格中打断单词

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 

@Caveatrob:还假设您在空格上断开:

<%  
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)  
%>  
0那么
aWords=拆分(单字,“”)
对于iCounter=0到UBound(aWords)
如果Len(aWords(iCounter)和“”)+Len(sTmp)

以下两个答案中的任何一个对您有用吗?:-)