dim strS as string,strF as string,i as integer,intCount as integer strS="FDFSDFDFDFEEEENSDED" strF="F" for i=0 to len(strS) if mid(strS,i,1)=strF then intCount=intCount+1 nextdebug.print intCount楼主想要这个吗?
Dim strS1 As String, strS2 As String, strF As String, intCount As Integer strS1 = "FDFSDFDFDFEEEENSDED" strF = "F" strS2 = Replace(strS1, strF, "") intCount = Len(strS1) - Len(strS2)
Debug.Print intCount這樣OK嗎?
如何查询一个text里 出现的字符的个数?我用了InStr方法无法实现 还有其他方法吗? --------------------------------- dim i,s s="FDFSDFDFDFEEEENSDED" i=ubound(split(s,"F"))
vbman2003(家人)方法不错啊,呵呵
dim i,s s="FDFSDFDFDFEEEENSDED" i=ubound(split(s,"F"))这个好
Option ExplicitPublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Public Function WordCountGetByTable(ByRef pTable() As Long, ByVal pString As String) As Long '在频率表里查询一个字符出现的次数。 Dim tOutCount As Long Dim tTable_Index As Long
WordCountGetByTable = tOutCount End FunctionPublic Function WordTableGetInfoText(ByRef pTable() As Long) As String '得到一个字符串表示的频率表,以供查看之用。 Dim tOutText As String Dim tTable_Index As Long For tTable_Index = -32768 To 32767 If CBool(pTable(tTable_Index)) Then tOutText = tOutText & ChrW(tTable_Index) & ":" & pTable(tTable_Index) & vbCrLf End If Next WordTableGetInfoText = tOutText End FunctionPublic Function WordTableGetByText(ByRef pText As String) As Long() '取得一个字符串的频率表。(pText必须是一个存在字符的字符串,不能为空) Dim tOutTable() As Long Dim tOutTable_Index As Integer
ReDim tOutTable(-32769 To 32767)
Dim tTextBytes() As Byte Dim tTextBytes_Length As Long Dim tTextBytes_Count As Long
CopyMemory tTextCodes(0), tTextBytes(0), tTextBytes_Count Dim tTextCodes_Index As Long For tTextCodes_Index = 0 To tTextCodes_Length tOutTable_Index = tTextCodes(tTextCodes_Index) tOutTable(tOutTable_Index) = tOutTable(tOutTable_Index) + 1 Next
clear_zero(清晰) dim i,s s="FDFSDFDFDFEEEENSDED" i=ubound(split(s,"F"))这个好这个好
Private Function InStrCount(ByVal Source As String, ByVal Find As String) As Long If Len(source) > 0 Then InStrCount = UBound(Split(Source, Find)) End If End FunctionPublic Function InStrCount( _ SourceString As String, _ FindString As String, _ Optional ByVal Start As Long = 1, _ Optional CompareType As VbCompareMethod = vbBinaryCompare) As Long ' Dim l As Long ' l = Len(FindString) ' If l > 0 Then ' If Start < 1 Then Start = 1 End If ' Do Start = InStr(Start, SourceString, FindString, CompareType) If Start Then InStrCount = InStrCount + 1 Start = Start + l Else Exit Function End If Loop End If ' End Function
strS="FDFSDFDFDFEEEENSDED"
strF="F"
for i=0 to len(strS)
if mid(strS,i,1)=strF then intCount=intCount+1
nextdebug.print intCount楼主想要这个吗?
strS1 = "FDFSDFDFDFEEEENSDED"
strF = "F"
strS2 = Replace(strS1, strF, "")
intCount = Len(strS1) - Len(strS2)
Debug.Print intCount這樣OK嗎?
还有其他方法吗?
---------------------------------
dim i,s
s="FDFSDFDFDFEEEENSDED"
i=ubound(split(s,"F"))
s="FDFSDFDFDFEEEENSDED"
i=ubound(split(s,"F"))这个好
'在频率表里查询一个字符出现的次数。
Dim tOutCount As Long
Dim tTable_Index As Long
tTable_Index = AscW(pString)
tOutCount = pTable(tTable_Index)
WordCountGetByTable = tOutCount
End FunctionPublic Function WordTableGetInfoText(ByRef pTable() As Long) As String
'得到一个字符串表示的频率表,以供查看之用。
Dim tOutText As String
Dim tTable_Index As Long
For tTable_Index = -32768 To 32767
If CBool(pTable(tTable_Index)) Then
tOutText = tOutText & ChrW(tTable_Index) & ":" & pTable(tTable_Index) & vbCrLf
End If
Next
WordTableGetInfoText = tOutText
End FunctionPublic Function WordTableGetByText(ByRef pText As String) As Long()
'取得一个字符串的频率表。(pText必须是一个存在字符的字符串,不能为空)
Dim tOutTable() As Long
Dim tOutTable_Index As Integer
ReDim tOutTable(-32769 To 32767)
Dim tTextBytes() As Byte
Dim tTextBytes_Length As Long
Dim tTextBytes_Count As Long
tTextBytes() = pText
tTextBytes_Length = UBound(tTextBytes())
tTextBytes_Count = tTextBytes_Length + 1
Dim tTextCodes() As Integer
Dim tTextCodes_Length As Long
tTextCodes_Length = ((tTextBytes_Count) \ 2) - 1
ReDim tTextCodes(0 To tTextCodes_Length)
CopyMemory tTextCodes(0), tTextBytes(0), tTextBytes_Count Dim tTextCodes_Index As Long
For tTextCodes_Index = 0 To tTextCodes_Length
tOutTable_Index = tTextCodes(tTextCodes_Index)
tOutTable(tOutTable_Index) = tOutTable(tOutTable_Index) + 1
Next
WordTableGetByText = tOutTable()
End Function
Dim tTable() As Long
tTable() = WordTableGetByText(Text1.Text)
Text1.Text = WordTableGetInfoText(tTable())
Text2.Text = WordCountGetByTable(tTable(), "t")
End SubText1.Text是一个Muntiline=True的多行文本框。将我上面提供的函数模块代码输入到文本框里,得到下面的结果:Text1.Text显示输入文本的字符频率表(所有出现字符每个字符出现的次数):能:1
表:4
询:1
里:1
须:1
频:3
,:2:59:59
:246
":6
&:4
':3
(:30
):30
+:2
,:5
-:3
0:4
1:3
2:6
3:5
6:4
7:6
8:1
9:1
::1
=:14
A:26
B:23
C:25
D:16
E:5
F:8
G:6
I:21
L:23
M:4
N:2
O:17
P:4
R:6
S:8
T:66
U:1
V:2
W:8
\:1
_:26
a:33
b:35
c:14
d:37
e:135
f:8
g:29
h:10
i:37
k:1
l:40
m:17
n:79
o:65
p:14
r:24
s:47
t:151
u:38
v:2
x:51
y:28
。:3
一:4
不:1
个:4
串:3
为:1
之:1
以:1
供:1
出:1
到:1
取:1
在:2
字:5
存:1
得:2
必:1
数:1
是:1
查:2
次:1
率:3
现:1
用:1
的:4
看:1
示:1
空:1
符:5Text2.text显示小写字母t的出现次数。151从算法角度来说,取一个字母的出现频率与取所有字母的出现频率都需要历遍一次字符串,两者速度几乎是相同的。
Dim n
For i = 1 To Len(Text4.Text)
n = n + 1
Next
MsgBox n
Dim n
For i = 1 To Len(Text4.Text)
n = n + 1
Next
MsgBox n-------------------------
msgbox Len(text4.text) 不是直接些嗎......
2。jason_lu(吾系靚仔) 判断单个字符。
3。KiteGirl(小仙妹)复杂,但全面。
得到文本的频率之后,可以根据频率排序重新编码文本。将频率高的编码为数值小的编码;将频率小的编码为数值高的编码。这样,那些出现频率多的字符可以用比较少的bit表示。然后用哈夫曼编码来表示这种被重新编码的文本,可以减少存储空间。是一种文本的压缩算法。
如果根据频率重新定义字库的排列,只把文本出现的字加入到出版物里。这样就是一个比较不错的电子出版物格式。由于编码被重新排列,只能用对应排列的内置字库来显示出正确的文本。如果不给出编码时候产生的解码表的话,不能再被直接换算成原来的文本(除非用OCR技术),因此这种格式本身对版权的保护有一定效果。
dim i,s
s="FDFSDFDFDFEEEENSDED"
i=ubound(split(s,"F"))这个好这个好
If Len(source) > 0 Then
InStrCount = UBound(Split(Source, Find))
End If
End FunctionPublic Function InStrCount( _
SourceString As String, _
FindString As String, _
Optional ByVal Start As Long = 1, _
Optional CompareType As VbCompareMethod = vbBinaryCompare) As Long
'
Dim l As Long
'
l = Len(FindString)
'
If l > 0 Then
'
If Start < 1 Then
Start = 1
End If
'
Do
Start = InStr(Start, SourceString, FindString, CompareType)
If Start Then
InStrCount = InStrCount + 1
Start = Start + l
Else
Exit Function
End If
Loop
End If
'
End Function