Split
是String
类的方法,而不是TextBox
类。因此,无论您从哪里获取字符串,无论是来自文本框、自定义控件、文件还是其他任何地方,都可以使用该String.Split
方法来拆分它。例如:
Dim v1 As Int32
Dim v2 As Int32
Dim rule As String = TextBox1.Text
Dim parts() As String = rule.Split(New String() {" To "}, StringSplitOptions.None)
v1 = Integer.Parse(parts(0))
v2 = Integer.Parse(parts(1))
MessageBox.Show(v1 & " " & v2)
或者,更简洁:
' ...
Dim parts() As String = TextBox1.Text.Split(New String() {" To "}, StringSplitOptions.None)
' ...
要使拆分不区分大小写,只需在拆分之前对整个字符串强制使用一种或另一种方式,例如:
Dim parts() As String = TextBox1.Text.ToLower().Split(New String() {" to "}, StringSplitOptions.None)
或者
Dim parts() As String = TextBox1.Text.ToUpper().Split(New String() {" TO "}, StringSplitOptions.None)