您的位置:程序门 -> vb -> 基础类



如何查询一个text里 出现的字符的个数? 我用了instr方法无法实现


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何查询一个text里 出现的字符的个数? 我用了instr方法无法实现[已结贴,结贴人:zhizlm]
发表于:2007-04-11 16:06:29 楼主
如何查询一个text里   出现的字符的个数?我用了instr方法无法实现  


还有其他方法吗?
发表于:2007-04-11 16:13:311楼 得分:0
用instr可以实现,循环找或者递归找都可以,每次都以上一次找到的位置为起点,直到返回0为止。
发表于:2007-04-11 16:27:422楼 得分:20
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
next

debug.print   intcount

楼主想要这个吗?
发表于:2007-04-11 16:33:363楼 得分:0
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嗎?
发表于:2007-04-11 16:39:004楼 得分:0
如何查询一个text里   出现的字符的个数?我用了instr方法无法实现  


还有其他方法吗?
---------------------------------
dim   i,s
s= "fdfsdfdfdfeeeensded "
i=ubound(split(s, "f "))
发表于:2007-04-11 16:53:395楼 得分:0
vbman2003(家人)方法不错啊,呵呵
发表于:2007-04-11 17:31:026楼 得分:0
最好问题描述得再详细一点
发表于:2007-04-11 17:39:207楼 得分:0
dim   i,s
s= "fdfsdfdfdfeeeensded "
i=ubound(split(s, "f "))

这个好
发表于:2007-04-11 17:58:348楼 得分:0
等我一会教你一个厉害的办法,比楼上那些人好用……
发表于:2007-04-11 18:37:439楼 得分:0
option   explicit

public   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
   
    ttable_index   =   ascw(pstring)
    toutcount   =   ptable(ttable_index)
   
    wordcountgetbytable   =   toutcount
end   function

public   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   function

public   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

发表于:2007-04-11 18:45:1310楼 得分:0
测试代码:

private   sub   command1_click()
    dim   ttable()   as   long
    ttable()   =   wordtablegetbytext(text1.text)
    text1.text   =   wordtablegetinfotext(ttable())
    text2.text   =   wordcountgetbytable(ttable(),   "t ")
end   sub

text1.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
符:5

text2.text显示小写字母t的出现次数。

151

从算法角度来说,取一个字母的出现频率与取所有字母的出现频率都需要历遍一次字符串,两者速度几乎是相同的。
发表于:2007-04-11 20:19:5011楼 得分:0
最简单的方法
dim   n
for   i   =   1   to   len(text4.text)
n   =   n   +   1
next
msgbox   n
发表于:2007-04-12 08:30:0912楼 得分:0
最简单的方法
dim   n
for   i   =   1   to   len(text4.text)
n   =   n   +   1
next
msgbox   n

-------------------------
msgbox   len(text4.text)   不是直接些嗎......
发表于:2007-04-12 08:38:4313楼 得分:0
简单就是最好的!
发表于:2007-04-12 10:44:5614楼 得分:0
1。vbman2003(家人)的解答很smart,也对。
2。jason_lu(吾系靚仔)   判断单个字符。
3。kitegirl(小仙妹)复杂,但全面。

发表于:2007-04-12 11:17:3915楼 得分:0
查询某个字符出现的频率只是字符频率表的一个很简单的用法。实际上那个频率表函数是在大学时候研究一个电子出版物文档编码时候做的。
得到文本的频率之后,可以根据频率排序重新编码文本。将频率高的编码为数值小的编码;将频率小的编码为数值高的编码。这样,那些出现频率多的字符可以用比较少的bit表示。然后用哈夫曼编码来表示这种被重新编码的文本,可以减少存储空间。是一种文本的压缩算法。
如果根据频率重新定义字库的排列,只把文本出现的字加入到出版物里。这样就是一个比较不错的电子出版物格式。由于编码被重新排列,只能用对应排列的内置字库来显示出正确的文本。如果不给出编码时候产生的解码表的话,不能再被直接换算成原来的文本(除非用ocr技术),因此这种格式本身对版权的保护有一定效果。
发表于:2007-04-12 11:47:1316楼 得分:0
clear_zero(清晰)          
dim   i,s
s= "fdfsdfdfdfeeeensded "
i=ubound(split(s, "f "))

这个好

这个好

   
 
发表于:2007-04-12 11:59:4217楼 得分:0
小仙妹功底扎实,佩服一个
发表于:2007-04-12 12:56:1718楼 得分:0
mark
发表于:2007-04-12 13:48:3319楼 得分:0
凡有小仙妹的帖子就收藏,呵呵
发表于:2007-04-12 16:29:0620楼 得分:0
kitegirl(小仙妹)好厲害啊...
发表于:2007-04-13 17:17:5421楼 得分:0
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   function

public   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


快速检索

最新资讯
热门点击