PDA

View Full Version : How to use a substring to separate words in visual basic


lindsey11
Dec 21, 2009, 06:36 PM
I am trying to write a program in visual basic. The user types in a name and then I display the name in two different labels. One label with the first name one with the second, but I do not know how to use the substring method to separate the two words. Into two different strings. So far I have this.
'delcare variables

Dim strfullname As String

Dim strFirst As String
Dim strsecond As String
Dim intfirst As Integer

'assign input to variables

strfullname = Convert.ToString(Me.txtname.Text)

Do Until strfullname = ""

strfullname.Substring(intfirst(“ “), 1)
intfirst = (intfirst + 1)
Loop

strFirst = strfullname.Substring(0, intfirst)
strsecond = strfullname.Substring(intfirst)

lindsey11
Dec 21, 2009, 07:13 PM
Please I need help asap!

lindsey11
Dec 21, 2009, 09:38 PM
Or if there is any alternate way please let me know!

Perito
Dec 22, 2009, 03:08 PM
You need patience. We can't always be here exactly when you want us.

First of all, congratulations on being able to spell SEPARATE. Most people, nowadays, spell it SEPERATE, which is incorrect.

The "Convert" and "Substring" functions suggests that this is Visual Basic.NET. Is that what you're writing in? If not, the solution will be different than if it's Visual Basic 6.0 or earlier.

If you have to use the SubString command then you need something to show you where the space is. This is the ".IndexOf" method. The Substring method takes two parameters: 1) the starting position (the first character is at position 0), 2) the number of characters to include in the sub-string. You have to be careful not to specify more characters than there actually are. If you do, the runtime will throw an error.

This would be how to do it:

'declare variables
Dim strfullname As String = ""
Dim strFirst As String = ""
Dim strsecond As String = ""
Dim spacePosn As Integer
'assign input to variables

' The trim function will eliminate leading and trailing spaces.
' A leading space will cause a problem. The simplest way to deal
' with it is to eliminate leading spaces.
' The Convert function isn't required because txtName.Text is already a string.
strfullname = Convert.ToString(Me.txtName.Text).Trim
'strfullname = Me.txtName.text.trim ' alternate
'strfullname = me.txtname.text.tostring.trim ' this would be preferable to "Convert", in my opinion
spacePosn = strfullname.IndexOf(" "c) ' This is the position of the space.

If spacePosn > 0 Then
strFirst = strfullname.Substring(0, spacePosn)
strsecond = strfullname.Substring(spacePosn + 1, strfullname.Length - spacePosn - 1)
Else
strFirst = strfullname
strsecond = "" ' there's no last name entered.
End If
txtFirst.Text = strFirst
txtLast.Text = strsecond


An alternate way to do it, without using the Substring function is to do the ".Split" command. It "splits" a string into its substrings using the argument as the separators. In this case, I've specified the space as a separator.

Dim strfullname As String

strfullname = Convert.ToString(Me.txtName.Text)
Dim Pieces() As String = strfullname.Split(" "c)
If Pieces.Length > 0 Then
txtFirst.Text = Pieces(0)
Else
txtFirst.Text = ""
End If
If Pieces.Length > 1 Then
txtLast.Text = Pieces(1)
Else
txtLast.Text = ""
End If

ScottGem
Dec 22, 2009, 03:15 PM
The better alternative is to put separate controls on your form For the different parts of a name. The problem here is that names don't always file a pattern. In the code Perito gave you, the result depends on the user entering a first name with a space then a last name. But what if the person's name is Mary Ann Smith. The result of the code would be:
Fname: Mary
Lname: Ann Smith

You are almost always better off having the user enter data in their smallest elements. It's a lot easier to put the data together then break it apart.

Perito
Dec 22, 2009, 09:04 PM
ScottGem is correct. It's a whole lot better to have the user enter data in fields than it is to guess at what the fields should be. However, I think the assignment is intended to learn to parse strings.

lindsey11
Dec 30, 2009, 11:49 AM
Thank you for your help

ScottGem
Dec 30, 2009, 12:35 PM
thank you for your help

So was this for homework? Frankly I would give the instructor the solution but then tell him that its not the best solution.

I teach a course on Access and the text I use is good, but it has some things where I disagree with how the text does it. I ask my students if they think the text's solution is the best idea.