0
我想在vbScript(Classic ASP)中将字符串解析为指定的长度而不用切分单词。最终的字符串可能小于指定的长度(以避免切词),但不应超过它。vbscript全字返回字符串<=指定限制
我想在vbScript(Classic ASP)中将字符串解析为指定的长度而不用切分单词。最终的字符串可能小于指定的长度(以避免切词),但不应超过它。vbscript全字返回字符串<=指定限制
假设你在空格上打断单词。
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)
%>
无论有没有下面的帮助/工作的两个答案给你的? :-) – stealthyninja 2010-11-30 20:25:35