-
Notifications
You must be signed in to change notification settings - Fork 6
/
strUtils.vbs
63 lines (55 loc) · 1.86 KB
/
strUtils.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'-----------------------------------------------------------------------------------------------------------------------
'
' Copyright (c) Ilya Kisleyko. All rights reserved.
'
' AUTHOR : Ilya Kisleyko
' E-MAIL : [email protected]
' DATE : 01.08.2015
' NAME : strUtils.vbs
' COMMENT : Several functions for working with words, idea based on RXLib's strUtils.pas
'
' 1) Function WordCount(sIn, aDelims) : given a set of word delimiters, returns number of words in sIn.
' WScript.Echo WordCount("Hello, How are# you! today?", caDelims)
' =5
'2) Function ExtractWord (iPos, sIn, aDelims) : returns the iPos'th word in sIn
' WScript.Echo ExtractWord(0, "Hello, How are# you! today?", caDelims)
' = "Hello"
Option Explicit
On Error Resume Next
'default set of word delimiters
dim caDelims
caDelims = Array(".", ",", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "|", "\", "/", "?", ":", ";", "(", ")", "{", "}", """", "'")
Function NormalizeString(sIn, aDelims)
dim i,sOut, aOut, iMax
'çàìåíÿåì ñèìâîëû-ðàçäåëèòåëè íà ïðîáåëû
sOut = sIn
for i=LBound(aDelims) to UBound(aDelims)
sOut = Replace(sOut, aDelims(i), " ")
next
'ïðåîáðàçîâûâàåì â ìàññèâ ïî ðàçäåëèòåëÿì (ïðîáåëàì)
aOut = split(sOut, " ")
'ñîáèðàåì ñòðîêó îáðàòíî
sOut = ""
iMax = UBound(aOut)
for i=LBound(aOut) to iMax
if aOut(i) <> "" then
sOut = sOut + aOut(i) + " "
End if
next
'îáðåçàåì ëèøíèé ïðîáåë â êîíöå
NormalizeString = Trim(sOut)
End Function
Function WordCount(sIn, aDelims)
'WordCount given a set of word delimiters, returns number of words in sIn.
dim sOut, aOut
sOut = NormalizeString (sIn, aDelims)
aOut = split(sOut, " ")
WordCount = UBound(aOut) + 1
End Function
Function ExtractWord (iPos, sIn, aDelims)
'returns the iPos'th word in sIn
dim sOut, aOut
sOut = NormalizeString (sIn, aDelims)
aOut = split(sOut, " ")
ExtractWord = aOut(iPos)
End Function