像这样的用户输入
textbox1.text = 01/02/03/......
我想像这样在3个文本框中分别显示值
在"/"
它应该移动到下一行之后
txt1.text = 01
txt2.text = 02
txt3.text = 03
....
这该怎么做。
需要vb.net代码帮助
选项1
如果总是 3 个文本框,您可以为每个文本框编写静态代码,如下所示:
'EDIT: This code now checks for the existence of a second or third value to avoid
'out of bounds errors
Dim originalValue As String = "01/02/03"
Dim splitBySlash As String() = originalValue.Split("/")
txt1.Text = splitBySlash(0)
If splitBySlash.Length > 1 Then txt2.Text = splitBySlash(1)
If splitBySlash.Length > 2 Then txt3.Text = splitBySlash(2)
选项 2
如果基于斜杠的文本框数量可变,则必须在运行时创建它们并将它们添加到父控件,如下所示:
'You can enter as many (or few) slashes as you like in this code, it will automatically
'adjust the text boxes created as necessary.
Dim originalValue As String = "01/02/03" 'could go on like /04/05/etc
Dim splitBySlash As String() = originalValue.Split("/")
For Each value As String In splitBySlash
Dim newTxt As New TextBox()
newTxt.Text = value
yourParentControl.Controls.Add(newTxt)
Next
试试这个:
string rockString = "01/02/03/";
string[] words = rockString.Split('/');
foreach (string word in words)
{
Console.WriteLine(word);
}
正如你在评论中问的
在不同的文本框中
textbox1.text = words[0]; //textbox1.text="01";
textbox2.text = words[2]; //textbox2.text="02";
textbox3.text = words[3]; //textbox3.text="03";
在同一个文本框中
textbox1.text = words[0]+words[1]+words[2];
尝试使用String.Split或 Regex.Split
Dim value As String = "01//02//03//"
Dim lines As String() = Regex.Split(value, "//")
For Each line As String In lines
Console.WriteLine(line)
Next