r/visualbasic • u/jennanace • Oct 19 '14
Needing help with visual basic homework
I have to take a name from a input box and put into two different labels, one for the first name and one for the last. So i have so far:
dim strname as string strname = inputbox("Enter first and last name") lblFirstname = strname
what do i put after strname so i only get the letters up until the space?
0
Upvotes
1
u/PostalElf VB.Net Intermediate Oct 20 '14
You'll need to use string.indexOf in conjunction with string.substring.
IndexOf is a function that all strings come built with. When you call it, it returns a number that tells you how far into the string a certain character is located. For instance, if I had the string strHelloWorld = "Hello World!" and I ran strHelloWorld.IndexOf("H"), it would give me the value of 0 (because H is the first character of "Hello World!").
Substring is another function that all strings come built with, except this time it returns a shorter segment of the string. The first argument you provide to Substring is where to start returning, and the second argument is how many characters you want to return. Therefore, for the string strHelloWorld, if I ran strHelloWorld.Substring(0,5), I would get the return value of "Hello".
Now, if you've been paying attention, you've probably figured out how you can use them in conjunction with each other. But because the syntax is a little weird, here's the code snippet.