Ask Experts Questions for FREE Help !
Ask
    lindsey11's Avatar
    lindsey11 Posts: 4, Reputation: 1
    New Member
     
    #1

    Dec 21, 2009, 06:36 PM
    how to use a substring to separate words in visual basic
    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's Avatar
    lindsey11 Posts: 4, Reputation: 1
    New Member
     
    #2

    Dec 21, 2009, 07:13 PM

    Please I need help asap!
    lindsey11's Avatar
    lindsey11 Posts: 4, Reputation: 1
    New Member
     
    #3

    Dec 21, 2009, 09:38 PM

    Or if there is any alternate way please let me know!
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #4

    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's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #5

    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's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #6

    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's Avatar
    lindsey11 Posts: 4, Reputation: 1
    New Member
     
    #7

    Dec 30, 2009, 11:49 AM

    Thank you for your help
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #8

    Dec 30, 2009, 12:35 PM
    Quote Originally Posted by lindsey11 View Post
    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Visual basic [ 1 Answers ]

Dim first As Long Dim second As Long Dim sign As String Dim FirstValueEntered As Boolean Private Sub Command1_Click(Index As Integer) If FirstValueEntered = True Then TextDisplay.Text = TextDisplay.Text & Command1(Index).Caption second = Val(TextDisplay.Text)

Visual basic [ 1 Answers ]

I want to to create a calculator in visual basic. How can I do this.there only that key preasent what I know that is (0-9, +, -, *, /,. = cleare)this code are preasent. So tell me the code which code I use to create this buttons.

Visual Basic [ 1 Answers ]

Actually I'm a new user(student) of Visual Basic.So I want to know how I am make a simple calculator in Visual Basic 6?

Hello visual basic [ 3 Answers ]

I really need help passing a course in school that has to do with the book An introduction to programming using Microsoft visual basic.net

Visual basic [ 1 Answers ]

How can I disable maximise button of a form but minimise and close button should be enable?


View more questions Search