[英]Count number of characters after specific character in textbox
using winforms / vb.net 使用winforms / vb.net
I am trying to count how many characters exist in "textbox3" after a specific character "." 我试图计算在特定字符“。”之后“textbox3”中存在多少个字符。 in a textbox. 在文本框中。
examples: 例子:
2adf = 0 (no "." exists) 2adf = 0(不存在“。”)
2adf. 2adf。 = 0 = 0
2adf.2 = 1 2adf.2 = 1
2adf.2a = 2 2adf.2a = 2
2adf.2af = 3 2adf.2af = 3
2adf.2afe = 4 2adf.2afe = 4
I already have a function to search if there is a "." 我已经有一个搜索是否有“。”的功能。
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
I am not sure how to check the count after the "." 我不知道如何检查“。”后的计数。 though 虽然
You could use the string.IndexOf method for this task 您可以使用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
Try this 尝试这个
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.