2014-12-20 67 views
-4

我有一个maxLength的文本框8.前两个字符必须是“PM”或“00”。我试过split(),但没有工作。将字词拆分为字符VB.net

+0

哪里是你的代码?输入示例,预期与实际结果?阅读http://stackoverflow.com/help/mcve和[编辑]您的问题 –

回答

1

使用substring()方法

Dim s As String = TextBox1.Text.Substring(0, 2) 

    If s = "PM" Or s = "00" Then 
     MessageBox.Show("good!") 
    Else 
     MessageBox.Show("bad!") 
    End If 
1

或者你可以使用StartsWith()

If TextBox1.Text.StartsWith("PM") OR TextBox1.Text.StartsWith("00") Then 
    'Do something 
End If 
0

也许你可以试试这个:

if textbox1.text like "PM*" or textbox1.text like "00*" then 
    Do something 
else msgbox("You don't have pm or 00 to start with!") 
end if 
+0

您需要用'Like'替换'='才能正常工作。 – Neolisk

+0

好的,谢谢! – thomasxd24

1

另一种选择是使用正则表达式:

Dim re As New Regex("PM|00") 
If re.IsMatch(TextBox1.Text) Then 
    'do something 
End If 

好处是,当您决定如何处理其他6个字符时,您可以修改上述内容来捕获并返回这些(全部或部分),而无需重写代码。您甚至可以在一个字符串中处理多次出现PM|00并将其全部捕获。

有用的资源,正则表达式沙箱:

+1

刚回来添加到我的答案。这将是最好的解决方案。 – Fred