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

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

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

因此就必須自行撰寫一個簡單的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

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *