簡體   English   中英

計算文本框中特定字符后的字符數

[英]Count number of characters after specific character in textbox

使用winforms / vb.net

我試圖計算在特定字符“。”之后“textbox3”中存在多少個字符。 在文本框中。

例子:

2adf = 0(不存在“。”)

2adf。 = 0

2adf.2 = 1

2adf.2a = 2

2adf.2af = 3

2adf.2afe = 4

我已經有一個搜索是否有“。”的功能。

if (CountCharacter(TextBox3.Text, ".") = 1) then
    'a "." exists so count number of characters after "."

end if

Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
    Dim cnt As Integer = 0
    For Each c As Char In value
        If c = ch Then cnt += 1
    Next
    Return cnt
End Function

我不知道如何檢查“。”后的計數。 雖然

您可以使用string.IndexOf方法執行此任務

Sub Main
    Dim test = "2adf.2afe"
    Dim result = CountCharsAfter(test, "."c)
    Console.WriteLine(result)
End Sub

Public Function CountCharsAfter(input as string, charToSearch as Char) as Integer
    DIm pos = input.LastIndexOf(charToSearch)
    if pos = -1 then
       return 0
    else
        return input.Length - (pos + 1)
    End if
End Function

嘗試這個

    Dim NoChar As Integer = CalculateChra("12adf.2afe", ".")

Private Function CalculateChra(ByVal V_String As String, ByVal LastChar As Char) As Integer

    Dim Start As String = Split(V_String, LastChar)(0) & "."
    Dim M As String = V_String.Substring(Start.Length)
    Return M.Length
End Function
dim n,cnt as integer
n=0
cnt=0
   Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If e.KeyChar = Chr(46) Then
                n = Len(TextBox1.Text)
            End If
            If n <> 0 Then
                cnt += 1
            End If
            MsgBox(" no.of charectors after '.' is/are : " & cnt - 1)
        End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM