分割字符串

You want to explode a delineated string (e.g. abc; cde; gef; xyz; abc).
Optionally, also get the unique values with no holes in the list...

An all LotusScript explode option is just using split:
linetext = "abc; cde; gef; xyz; abc"
rowVals = Split(linetxt, ";")


@Explode & @Trim Only:
Function LSExplode(strIn As String, sepstr As String) As Variant
' this function takes an incoming string (strIn) and turns it into an array separating via sepstr
' strIn - incoming string to be converted, e.g. abc;gef;abb;ddd
Dim evalstr As String ' the string combined for evaluate statement
Dim evalRtn As Variant ' the returned results of Evaluate
' check sepstr
If sepstr = "" Then
sepstr =";"
End If
' test strIn
If strIn="" Then
LSExplode = ""
Exit Function
Else
' lets do explode
evalstr = |@Trim(@Explode("| + strIn + |"; "| + sepstr + |"))|
evalRtn = Evaluate(evalstr)
End If
LSExplode = evalRtn
End Function

@Explode & @Unique:
Function LSExplode(strIn As String, sepstr As String) As Variant
' this function takes an incoming string (strIn) and turns it into an array separating via sepstr
' strIn - incoming string to be converted, e.g. abc;gef;abb;ddd
Dim evalstr As String ' the string combined for evaluate statement
Dim evalRtn As Variant ' the returned results of Evaluate
' check sepstr
If sepstr = "" Then
sepstr =";"
End If
' test strIn
If strIn="" Then
LSExplode = ""
Exit Function
Else
' lets do explode
evalstr = |@Trim(@Unique(@Explode("| + strIn + |"; "| + sepstr + |")))|
evalRtn = Evaluate(evalstr)
End If
LSExplode = evalRtn
End Function

@Explode & @Unique & @Implode:
Function LSUnique(strIn As String, sepstr As String) As String
' this function takes an incoming string (strIn) and turns it into an array separating via sepstr
' this function with @unique also returns only the unique elements of the array
' strIn - incoming string to be converted, e.g. abc;gef;abb;ddd
Dim evalstr As String ' the string combined for evaluate statement
Dim evalRtn As Variant ' the returned results of Evaluate
' check sepstr
If sepstr = "" Then
sepstr =";"
End If
' test strIn
If strIn="" Then
LSUnique = ""
Exit Function
Else
' lets do explode
evalstr = |@Implode(@Trim(@Unique(@Explode("| + strIn + |"; "| + sepstr + |"))); "<br>")|
evalRtn = Evaluate(evalstr)
End If
LSUnique = evalRtn(0)
End Function

Function Explode (Byval WordList As String, Sep As String) As Variant

Dim SepLen As Integer
Dim WordLocation As Integer
Dim WordLen As Integer
Dim SubWordLen As Integer
Dim InstanceCount As Integer
Dim TempWordList As String
Dim endofstring As Integer
SepLen = Len(Sep)

WordList = Trim(WordList)
endofstring = Len(WordList)
test = Right(WordList, 1)
If (Right(WordList, 1) = ";") Then
TempWordList = Left( WordList, endofstring - 1 )
Else
TempWordList = WordList
End If

InstanceCount = 0

WordLocation = Instr(TempWordList, Sep)
While WordLocation > 0
WordLen = Len(TempWordList)
SubWordLen = (WordLocation-1)+SepLen
TempWordList = Right(TempWordList, WordLen - SubWordLen)
WordLocation = Instr(TempWordList, Sep)
InstanceCount = InstanceCount+1
Wend

Redim ReturnList(InstanceCount)

WordLocation = Instr(WordList, Sep)
For i = 0 To InstanceCount
If WordLocation = 0 Then
Word = WordList
ReturnList(i) = Word
Exit For
Else
Word = Left(WordList, (WordLocation - 1))
End If
ReturnList(i) = Word
WordLen = Len(WordList)
SubWordLen = (WordLocation-1)+SepLen
WordList = Right(WordList, WordLen - SubWordLen)
WordLocation = Instr(WordList, Sep)
Next i

Explode = ReturnList

End Function

你可能感兴趣的:(字符串)