以最大长度和特定字符切割字符串

时间:2019-11-24 21:14:27

标签: vbscript

我有一个字符串,其中包含用“;”分隔的值。不幸的是,该程序不能接受超过4000个字符。因此,如果len大于4000,我应该每减少4000个

问题是我无法削减价值。如果碰到“;”,我很幸运。

那么如何在不削减价值的情况下获得少于4000的收益呢?

1 个答案:

答案 0 :(得分:1)

您可以从位置4000开始并向后移动,直到找到逗号为止。这段代码创建了一个测试字符串,但是您可以将sValues设置为任何值:

Dim sValues
Dim iCounter

For iCounter = 1 To 1000
    If sValues <> "" Then sValues = sValues & ","
    sValues = sValues & "value" & iCounter
Next

If Len(sValues) > 4000 Then

    For iCounter = 4000 To 1 Step -1
        If Mid(sValues, iCounter, 1) = "," Then
            ' Last comma before 4000 is found
            sValues = Left(sValues, iCounter - 1)
            Exit For
        End If
    Next

End If

MsgBox Right(sValues, 10)

我显示最后10个字符以验证字符串是否被正确修剪。

另一种方法是使用Split function并建立字符串备份,直到达到4000。