vbscript返回整个单词的字符串< =指定的限制

时间:2010-09-17 03:53:53

标签: asp-classic vbscript

我希望在vbScript(Classic ASP)中将字符串解析为指定的长度,而不会剪切单词。最后一个字符串可能小于指定的长度(以避免剪切单词),但不应超过它..

2 个答案:

答案 0 :(得分:0)

假设你在空格上打断了话。

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 

答案 1 :(得分:0)

@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)  
%>