分享
 
 
 

用空格分割字符串的函数

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Option Explicit

'===============================================

'Words.bas - string handling functions for words

'Author: Evan Sims [esims@arcola-il.com]

'Based on a module by Kevin O'Brien

'Version - 1.2 (Sept. 1996 - Dec 1999)

'

'These functions deal with "words".

'Words = blank-delimited strings

'Blank = any combination of one or more spaces,

' tabs, line feeds, or carriage returns.

'

'Examples:

' pword("find 3 in here", 3) = "in" 3rd word

' words("find 3 in here") = 4 number of words

' split("here's /s more", "/s") = "more" Returns words after split identifier (/s)

' delWord("find 3 in here", 1, 2) = "in here" delete 2 words, start at 1

' midWord("find 3 in here", 1, 2) = "find 3" return 2 words, start at 1

' wordPos("find 3 in here", "in") = 3 word-number of "in"

' wordCount("find 3 in here", "in") = 1 occurrences of word "in"

' wordIndex("find 3 in here", "in") = 8 position of "in"

' wordIndex("find 3 in here", 3) = 8 position of 3rd word

' wordIndex("find 3 in here", "3") = 6 position of "3"

'wordLength("find 3 in here", 3) = 2 length of 3rd word

'

'Difference between Instr() and wordIndex():

' InStr("find 3 in here", "in") = 2

' wordIndex("find 3 in here", "in") = 8

'

' InStr("find 3 in here", "her") = 11

' wordIndex("find 3 in here", "her") = 0

'===============================================

Public Function Pword(ByVal sSource As String, _

n As Long) As String

'=================================================

' Word retrieves the nth word from sSource

' Usage:

' Word("red blue green ", 2) "blue"

'=================================================

Const SP As String = " "

Dim pointer As Long 'start parameter of Instr()

Dim pos As Long 'position of target in InStr()

Dim x As Long 'word count

Dim lEnd As Long 'position of trailing word delimiter

sSource = CSpace(sSource)

'find the nth word

x = 1

pointer = 1

Do

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 = Len(sSource) + 1 ' or if its the last word

Pword = Mid$(sSource, pointer, 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 word

Loop

End Function

Public Function Words(ByVal sSource As String) As Long

'=================================================

' Words returns the number of words in a string

' Usage:

' Words("red blue green") 3

'=================================================

Const SP As String = " "

Dim lSource As Long 'length of sSource

Dim pointer As Long 'start parameter of Instr()

Dim pos As Long 'position of target in InStr()

Dim x As Long 'word count

sSource = CSpace(sSource)

lSource = Len(sSource)

If lSource = 0 Then Exit Function

'count words

x = 1

pointer = 1

Do

Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces

pointer = pointer + 1

Loop

pos = InStr(pointer, sSource, SP) 'find next space

If pos = 0 Then Exit Do 'no more words

x = x + 1 'increment word counter

pointer = pos + 1 'start of next word

Loop

If Mid$(sSource, lSource, 1) = SP Then x = x - 1 'adjust if trailing space

Words = x

End Function

Public Function WordCount(ByVal sSource As String, _

sTarget As String) As Long

'=====================================================

' WordCount returns the number of times that

' word, sTarget, is found in sSource.

' Usage:

' WordCount("a rose is a rose", "rose") 2

'=================================================

Const SP As String = " "

Dim pointer As Long 'start parameter of Instr()

Dim lSource As Long 'length of sSource

Dim lTarget As Long 'length of sTarget

Dim pos As Long 'position of target in InStr()

Dim x As Long 'word count

lTarget = Len(sTarget)

lSource = Len(sSource)

sSource = CSpace(sSource)

'find target word

pointer = 1

Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces

pointer = pointer + 1

Loop

If pointer > lSource Then Exit Function 'sSource contains no words

Do 'find position of sTarget

pos = InStr(pointer, sSource, sTarget)

If pos = 0 Then Exit Do 'string not found

If Mid$(sSource, pos + lTarget, 1) = SP _

Or pos + lTarget > lSource Then 'must be a word

If pos = 1 Then

x = x + 1 'word found

ElseIf Mid$(sSource, pos - 1, 1) = SP Then

x = x + 1 'word found

End If

End If

pointer = pos + lTarget

Loop

WordCount = x

End Function

Public Function WordPos(ByVal sSource As String, _

sTarget As String) As Long

'=====================================================

' WordPos returns the word number of the

' word, sTarget, in sSource.

' Usage:

' WordPos("red blue green", "blue") 2

'=================================================

Const SP As String = " "

Dim pointer As Long 'start parameter of Instr()

Dim lSource As Long 'length of sSource

Dim lTarget As Long 'length of sTarget

Dim lPosTarget As Long 'position of target-word

Dim pos As Long 'position of target in InStr()

Dim x As Long 'word count

lTarget = Len(sTarget)

lSource = Len(sSource)

sSource = CSpace(sSource)

'find target word

pointer = 1

Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces

pointer = pointer + 1

Loop

If pointer > lSource Then Exit Function 'sSource contains no words

Do 'find position of sTarget

pos = InStr(pointer, sSource, sTarget)

If pos = 0 Then Exit Function 'string not found

If Mid$(sSource, pos + lTarget, 1) = SP _

Or pos + lTarget > lSource Then 'must be a word

If pos = 1 Then Exit Do 'word found

If Mid$(sSource, pos - 1, 1) = SP Then Exit Do

End If

pointer = pos + lTarget

Loop

'count words until position of sTarget

lPosTarget = pos 'save position of sTarget

pointer = 1

x = 1

Do

Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces

pointer = pointer + 1

Loop

If pointer >= lPosTarget Then Exit Do 'all words have been counted

pos = InStr(pointer, sSource, SP) 'find next space

If pos = 0 Then Exit Do 'no more words

x = x + 1 'increment word count

pointer = pos + 1 'start of next word

Loop

WordPos = x

End Function

Public Function WordIndex(ByVal sSource As String, _

vTarget As Variant) As Long

'===========================================================

' WordIndex returns the byte position of vTarget in sSource.

' vTarget can be a word-number or a string.

' Usage:

' WordIndex("two plus 2 is four", 2) 5

' WordIndex("two plus 2 is four", "2") 10

' WordIndex("two plus 2 is four", "two") 1

'===========================================================

Const SP As String = " "

Dim sTarget As String 'vTarget converted to String

Dim lTarget As Long 'vTarget converted to Long, or length of sTarget

Dim lSource As Long 'length of sSource

Dim pointer As Long 'start parameter of InStr()

Dim pos As Long 'position of target in InStr()

Dim x As Long 'word counter

lSource = Len(sSource)

sSource = CSpace(sSource)

If VarType(vTarget) = vbString Then GoTo strIndex

If Not IsNumeric(vTarget) Then Exit Function

lTarget = CLng(vTarget) 'convert to Long

'find byte position of lTarget (word number)

x = 1

pointer = 1

Do

Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces

pointer = pointer + 1

Loop

If x = lTarget Then 'word-number of Target

If pointer > lSource Then Exit Do 'beyond end of sSource

WordIndex = pointer 'position of word

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

Loop

Exit Function

strIndex:

sTarget = CStr(vTarget)

lTarget = Len(sTarget)

If lTarget = 0 Then Exit Function 'nothing to count

'find byte position of sTarget (string)

pointer = 1

Do

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 + lTarget

Loop

WordIndex = pos

End Function

Public 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 sSource

Dim pointer As Long 'start parameter Instr()

Dim pos As Long 'position of target with InStr()

Dim x As Long 'word count

Dim lEnd As Long 'position of trailing word delimiter

sSource = CSpace(sSource)

lSource = Len(sSource)

'find the nth word

x = 1

pointer = 1

Do

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 word

Loop

End Function

Public 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 sTarget

Dim lSource As Long 'length of sSource

Dim pointer As Long 'start parameter of InStr()

Dim pos As Long 'position of target in InStr()

Dim x As Long 'word counter

Dim lStart As Long 'position of word n

Dim lEnd As Long 'position of space after last word

lSource = Len(sSource)

DelWord = sSource

sSource = CSpace(sSource)

If IsMissing(vWords) Then

lWords = -1

ElseIf IsNumeric(vWords) Then

lWords = CLng(vWords)

Else

Exit Function

End If

If n = 0 Or lWords = 0 Then Exit Function 'nothing to delete

'find position of n

x = 1

pointer = 1

Do

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 word

Loop

If lStart = 0 Then Exit Function

If lEnd = 0 Then

DelWord = Trim$(Left$(sSource, lStart - 1))

Else

DelWord = Trim$(Left$(sSource, lStart - 1) & Mid$(sSource, lEnd + 1))

End If

End Function

Public 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 long

Dim lSource As Long 'length of sSource

Dim pointer As Long 'start parameter of InStr()

Dim pos As Long 'position of target in InStr()

Dim x As Long 'word counter

Dim lStart As Long 'position of word n

Dim lEnd As Long 'position of space after last word

lSource = Len(sSource)

sSource = CSpace(sSource)

If IsMissing(vWords) Then

lWords = -1

ElseIf IsNumeric(vWords) Then

lWords = CLng(vWords)

Else

Exit Function

End If

If n = 0 Or lWords = 0 Then Exit Function 'nothing to delete

'find position of n

x = 1

pointer = 1

Do

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 word

Loop

If lStart = 0 Then Exit Function

If lEnd = 0 Then

MidWord = Trim$(Mid$(sSource, lStart))

Else

MidWord = Trim$(Mid$(sSource, lStart, lEnd - lStart))

End If

End Function

Public 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 Long

Dim pos As Long

Dim x As Long

Dim iSpace(3) As Integer

' define blank characters

iSpace(0) = 9 'Horizontal Tab

iSpace(1) = 10 'Line Feed

iSpace(2) = 13 'Carriage Return

iSpace(3) = 160 'Hard Space

CSpace = sSource

For 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

Loop

Next x

End Function

Public 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 If

End Function

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有