📄 qstring.bas
字号:
x = x + 1 'increment word counter pointer = pos + 1LoopExit FunctionstrIndex:sTarget = CStr(vTarget)lTarget = Len(sTarget)If lTarget = 0 Then Exit Function 'nothing to count'find byte position of sTarget (string)pointer = 1Do pos = InStr(pointer, sSource, sTarget) If pos = 0 Then Exit Do If Mid$(sSource, pos + lTarget, 1) = SP _ Or pos + lTarget > lSource Then If pos = 1 Then Exit Do If Mid$(sSource, pos - 1, 1) = SP Then Exit Do End If pointer = pos + lTargetLoopWordIndex = posEnd FunctionPublic Function WordLength(ByVal sSource As String, _ n As Long) As Long'=========================================================' Wordlength returns the length of the nth word in sSource' Usage:' WordLength("red blue green", 2) 4'=========================================================Const SP As String = " "Dim lSource As Long 'length of sSourceDim pointer As Long 'start parameter Instr()Dim pos As Long 'position of target with InStr()Dim x As Long 'word countDim lEnd As Long 'position of trailing word delimitersSource = CSpace(sSource)lSource = Len(sSource)'find the nth wordx = 1pointer = 1Do Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces pointer = pointer + 1 Loop If x = n Then 'the target word-number lEnd = InStr(pointer, sSource, SP) 'pos of space at end of word If lEnd = 0 Then lEnd = lSource + 1 ' or if its the last word WordLength = lEnd - pointer Exit Do 'word found, done End If pos = InStr(pointer, sSource, SP) 'find next space If pos = 0 Then Exit Do 'word not found x = x + 1 'increment word counter pointer = pos + 1 'start of next wordLoopEnd FunctionPublic Function DelWord(ByVal sSource As String, _ n As Long, _ Optional vWords As Variant) As String'===========================================================' DelWord deletes from sSource, starting with the' nth word for a length of vWords words.' If vWords is omitted, all words from the nth word on are' deleted.' Usage:' DelWord("now is not the time", 3) "now is"' DelWord("now is not the time", 3, 1) "now is the time"'===========================================================Const SP As String = " "Dim lWords As Long 'length of sTargetDim lSource As Long 'length of sSourceDim pointer As Long 'start parameter of InStr()Dim pos As Long 'position of target in InStr()Dim x As Long 'word counterDim lStart As Long 'position of word nDim lEnd As Long 'position of space after last wordlSource = Len(sSource)DelWord = sSourcesSource = CSpace(sSource)If IsMissing(vWords) Then lWords = -1ElseIf IsNumeric(vWords) Then lWords = CLng(vWords)Else Exit FunctionEnd IfIf n = 0 Or lWords = 0 Then Exit Function 'nothing to delete'find position of nx = 1pointer = 1Do Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces pointer = pointer + 1 Loop If x = n Then 'the target word-number lStart = pointer If lWords < 0 Then Exit Do End If If lWords > 0 Then 'lWords was provided If x = n + lWords - 1 Then 'find pos of last word lEnd = InStr(pointer, sSource, SP) 'pos of space at end of word Exit Do 'word found, done End If End If pos = InStr(pointer, sSource, SP) 'find next space If pos = 0 Then Exit Do 'word not found x = x + 1 'increment word counter pointer = pos + 1 'start of next wordLoopIf lStart = 0 Then Exit FunctionIf lEnd = 0 Then DelWord = Trim$(Left$(sSource, lStart - 1))Else DelWord = Trim$(Left$(sSource, lStart - 1) & Mid$(sSource, lEnd + 1))End IfEnd FunctionPublic Function MidWord(ByVal sSource As String, _ n As Long, _ Optional vWords As Variant) As String'===========================================================' MidWord returns a substring sSource, starting with the' nth word for a length of vWords words.' If vWords is omitted, all words from the nth word on are' returned.' Usage:' MidWord("now is not the time", 3) "not the time"' MidWord("now is not the time", 3, 2) "not the"'===========================================================Const SP As String = " "Dim lWords As Long 'vWords converted to longDim lSource As Long 'length of sSourceDim pointer As Long 'start parameter of InStr()Dim pos As Long 'position of target in InStr()Dim x As Long 'word counterDim lStart As Long 'position of word nDim lEnd As Long 'position of space after last wordlSource = Len(sSource)sSource = CSpace(sSource)If IsMissing(vWords) Then lWords = -1ElseIf IsNumeric(vWords) Then lWords = CLng(vWords)Else Exit FunctionEnd IfIf n = 0 Or lWords = 0 Then Exit Function 'nothing to delete'find position of nx = 1pointer = 1Do Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces pointer = pointer + 1 Loop If x = n Then 'the target word-number lStart = pointer If lWords < 0 Then Exit Do 'include rest of sSource End If If lWords > 0 Then 'lWords was provided If x = n + lWords - 1 Then 'find pos of last word lEnd = InStr(pointer, sSource, SP) 'pos of space at end of word Exit Do 'word found, done End If End If pos = InStr(pointer, sSource, SP) 'find next space If pos = 0 Then Exit Do 'word not found x = x + 1 'increment word counter pointer = pos + 1 'start of next wordLoopIf lStart = 0 Then Exit FunctionIf lEnd = 0 Then MidWord = Trim$(Mid$(sSource, lStart))Else MidWord = Trim$(Mid$(sSource, lStart, lEnd - lStart))End IfEnd FunctionPublic Function CSpace(sSource As String) As String'=================================================='CSpace converts blank characters'(ascii: 9,10,13,160) to space (32)'' cSpace("a" & vbTab & "b") "a b"' cSpace("a" & vbCrlf & "b") "a b"'==================================================Dim pointer As LongDim pos As LongDim x As LongDim iSpace(3) As Integer' define blank charactersiSpace(0) = 9 'Horizontal TabiSpace(1) = 10 'Line FeediSpace(2) = 13 'Carriage ReturniSpace(3) = 160 'Hard SpaceCSpace = sSourceFor x = 0 To UBound(iSpace) ' replace all blank characters with space pointer = 1 Do pos = InStr(pointer, CSpace, Chr$(iSpace(x))) If pos = 0 Then Exit Do Mid$(CSpace, pos, 1) = " " pointer = pos + 1 LoopNext xEnd FunctionPublic Function SplitString(iSource As String, iTarget As String, Optional BeforeTarget As Boolean = False) As String'=================================================='Returns the characters before or after the split'identifier. By default will return text after id,'set BeforeTarget as true to return the text before'it.'==================================================If BeforeTarget = True Then SplitString = DelWord(iSource, WordPos(iSource, iTarget))Else SplitString = DelWord(iSource, 1, WordPos(iSource, iTarget))End IfEnd Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -