[VB]判斷字串中某字元出現字數

0
103

某些情形下會需要判斷在該字串中某個字元出現的次數來進行後續判斷處理

而內建函數並沒有相關的函數可以使用

因此就必須自行撰寫一個簡單的function

程式碼如下

Public Function sCount(ByVal str As String, ByVal find As Char) As Integer           
  Dim count As Integer = 0             
    For Each s As Char In str                 
      If s = find Then                   
        count += 1                 
      End If            
    Next           
  Return count 
End Function

留下一個回覆

請輸入你的留言!
請在這裡輸入你的名字