注册 登录
编程论坛 Excel/VBA论坛

求助,请问怎么统计一段英文里首字母出现的次数

a88088 发布于 2021-01-07 22:46, 4590 次点击
要统计一段英文里首字母出现的次数,怎么做?下面这种的,
例如:Cryopreservation is the most efficient method for long-term preservation of mammalian sperm. However, freeze-thawing procedures may strongly impair the sperm function and survival and thus decrease the reproductive performance.
本段单词首字母出现次数:A:2 C:1 D:1 E:1 F:3 H:1 I:2 L:1 M:4 O:1 P:3 R:1 S:4 T:4
3 回复
#2
厨师王德榜2021-01-07 23:16
单词与单词之间一般是空格来分隔的,那么这一段话可以用split()先分割了,结果保存到一个数组。
遍历这个数组,找出数组中每项的首字母,再配合一个字典,就能获得了。
现在没时间写,不过,代码也很简单,你自己先摸索一下。
#3
hbra0002021-01-14 20:40
Function test(rg As Range) As String  '自定义公式 test ,选定单元格
    Dim d As Object
    Set d = CreateObject("scripting.dictionary")
    Dim arr
   
    arr = Split(Trim(rg), " ")
   
    Dim i As Long
    For i = 0 To UBound(arr)
        If Len(arr(i)) >= 1 Then
            d(UCase(Left(arr(i), 1))) = d(UCase(Left(arr(i), 1))) + 1
        End If
    Next i
   
    Erase arr
    If d.Count > 0 Then
        arr = d.keys
        For i = 0 To d.Count - 1
            If arr(i) <> " " Then
                test = test & " " & arr(i) & ":" & d(arr(i))
            End If
        Next i
    End If
    test = Right(test, Len(test) - 1)
End Function
#4
sdta2021-04-13 22:09
VFP CODE
程序代码:
CLEAR
TEXT TO lcStr NOSHOW TEXTMERGE
Cryopreservation is the most efficient method for long-term preservation of mammalian sperm. However, freeze-thawing procedures may strongly impair the sperm function and survival and thus decrease the reproductive performance.
ENDTEXT
ALINES(laStr, lcStr, 4 + 1, SPACE(1), ",", ".")
LOCAL la[26]
la = 0
FOR lnJ = 1 TO ALINES(laStr, lcStr, 4 + 1, SPACE(1), ",", ".")
    ln = ASC(PROPER(LEFT(laStr[lnJ], 1))) - 64
    la[ln] = la[ln] + 1
ENDFOR
FOR lnJ = 1 TO 26
    IF la[lnJ] > 0
        ? SPACE(5) + CHR(lnj + 64), la[lnj]
    ENDIF
ENDFOR


只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2021-4-13 22:10编辑过]

1