string VB 捕获文本框中的前 4 个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16257785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB-Catching first 4 characters in textbox
提问by Vladimar Chudnofski
I am currently working on a really basic scripting langauge (called EngineScript) that gets the first 4 letters of a textbox's content (in a Windows Form project) and compares it to a in-program list of keywords and then uses the remaining characters to form an argument to the keywords... I was wondering weather you had some code ideas on how to go about placing the first 4 letters in one variable and then the remaining characters into another. My langauge will be single command.. i.e you will only be able to run one command per program.
我目前正在研究一种非常基本的脚本语言(称为 EngineScript),它获取文本框内容的前 4 个字母(在 Windows 窗体项目中)并将其与程序内关键字列表进行比较,然后使用剩余的字符形成关键字的参数...我想知道您是否有一些关于如何将前 4 个字母放在一个变量中,然后将其余字符放入另一个变量的代码想法。我的语言将是单个命令.. 即每个程序只能运行一个命令。
回答by Andy
String.Substring()should work:
String.Substring()应该工作:
Dim first4Chars = TextBox1.Text.Substring(0, 4)
Dim restOfChars = TextBox1.Text.Substring(4)
回答by nckbrz
I will show you a good hint on how to get the first four letters, and see what you can put together with that. VB was a pain in my neck!! I struggled through it, and realized that is the only way to learn.. =)
我将向您展示如何获得前四个字母的好提示,看看您可以将它们放在一起。VB让我的脖子很痛!!我努力通过它,并意识到这是学习的唯一方法.. =)
dim substring as string
substringA = Left$("Entered String", 4)
strSubstrA = "Ente"
substringB = Left$("What about a space?", 6)
strSubstrB = "What a"
回答by RBarryYoung
something like this:
像这样:
Dim l4 as string, rest as string
l4 = LEFT(TextBox1.Text, 4)
rest = MID(TextBox1.Text, 5, LEN(TextBox1.Text))
回答by matzone
Your question is not too clear ..
你的问题不是太清楚..
But you can try this
但是你可以试试这个
dim sVar as string = mid(TextBox1.text,1,4)
回答by DiegoS
Carefull with Substring()function as it assumes that the textbox length is greater or equal than the second parameter. So if in your input someone puts less than 4 characters it will blow.
小心使用Substring()函数,因为它假定文本框长度大于或等于第二个参数。因此,如果在您的输入中有人输入少于 4 个字符,它会爆炸。
You should this to be safe:
你应该这样做是安全的:
Dim txt1 as String = Textbox1.Text
Dim first4Chars = If(txt1.Length > 4, txt1.Substring(0, 4), txt1.SubString(0,text.Length))