2010年9月17日 星期五

判斷中文字

'判斷字串中的中文字數和中文字
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ex As New Regex("[一-龥]")
Dim isMatched As Boolean = ex.IsMatch(TextBox4.Text)
Dim m As MatchCollection = ex.Matches(TextBox4.Text)
Dim chi As String = ""
Dim chiCount As Int16 = 0
For i As Int16 = 0 To m.Count - 1
chi &= m.Item(i).Value
chiCount = chiCount + 1
Next
MessageBox.Show("中文字數:" & chiCount & ",中文字:" & chi)
End Sub

'顯示字元碼
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MessageBox.Show(AscW(TextBox1.Text))
End Sub