none
Message from a parsed string RRS feed

  • Question

  • I got this code to work pretty well. The only problem I am having is that when I hit the Parse button it only says

    "User name:"

    Domain name:"

    It won't put whatever was entered in the textbox1 (email) in to the displayed message.

    Public Class Form1
    
        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    
        Private Sub btnParse_Click(sender As Object, e As EventArgs) Handles btnParse.Click
    
            Dim Email As String = txtEmail.Text
            Dim names As String() = Email.Trim.Split
            Dim username As String = ""
            Dim domainname As String = ""
            Dim chars As String = "@"
    
            If names.Length = 3 Then
                username = names(0)
                chars = names(1)
                domainname = names(2)
            End If
    
            If txtEmail.Text.Contains(chars) Then
                MessageBox.Show("User name: " & (username) & vbCrLf & "Domain name: " & (domainname))
            Else
                MessageBox.Show("Email needs to contain a @ symbol")
            End If
    
        End Sub
    
        Private Sub btnFormat_Click(sender As Object, e As EventArgs) Handles btnFormat.Click
            Dim City As String = txtCity.Text
            Dim State As String = txtState.Text
            Dim Letters As String = State.Substring(0).ToUpper
            Dim Zip As String = txtZipCode.Text
            Letters = Letters.Insert(Letters.Length, " ")
            State = Letters
            City = City.Insert(City.Length, ", ")
            MessageBox.Show("City, State, Zip:" & City & State & Zip)
        End Sub
    
    End Class

    Tuesday, November 6, 2018 9:32 PM

All replies

  • Change the dim names line to

            Dim names() As String = Email.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)

    see if that helps.  Also show what exactly was in txtEmail please.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Tuesday, November 6, 2018 9:45 PM
  • I tried it. It underlined the part of the line     {""c}

    When I moved the cursor over it, it said "Expression expected"

    Wednesday, November 7, 2018 3:33 AM
  • I tried it. It underlined the part of the line     {""c}

    When I moved the cursor over it, it said "Expression expected"


    Did you copy & paste that  line from the previous reply? Or did you try
    to type it all in?

    You removed the space from between the quotes:

    {" "c}

    - Wayne

    Wednesday, November 7, 2018 3:59 AM
  • It didn't cause an error but it didn't do what I wanted either. The form asks for an Email address.

    The code checks to make sure a @ exists then parses the email address into 2 parts and displays it -

    user name: xxxxxxx

    domain name: xxxxx.com

    I can't get the code to display the separate parts of the email. The way it is, it only displays -

    user name:

    domain name:

    Thanks

    Public Class Form1
    
        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    
        Private Sub btnParse_Click(sender As Object, e As EventArgs) Handles btnParse.Click
    
            Dim Email As String = txtEmail.Text
            Dim names() As String = Email.Trim.Split(CChar("@"))
            Dim userName As String = ""
            Dim domainName As String = ""
            Dim chars As String = "@"
    
            If names.Length = 3 Then
                userName = names(0)
                chars = names(1)
                domainName = names(2)
            End If
    
            If txtEmail.Text.Contains(chars) Then
                MessageBox.Show("User name: " & userName & vbCrLf & "Domain name: " & domainName, "Parsed String")
            Else
                MessageBox.Show("Email needs to contain a @ symbol", "Error Message")
            End If
    
        End Sub
    
        Private Sub btnFormat_Click(sender As Object, e As EventArgs) Handles btnFormat.Click
            Dim City As String = txtCity.Text
            Dim State As String = txtState.Text
            Dim Letters As String = State.Substring(0).ToUpper
            Dim Zip As String = txtZipCode.Text
            Letters = Letters.Insert(Letters.Length, " ")
            State = Letters
            City = City.Insert(City.Length, ", ")
            MessageBox.Show("City, State, Zip:" & City & State & Zip, "Formatted String")
        End Sub
    
    End Class

    Wednesday, November 7, 2018 8:11 PM
  • As previously requested, show an example of the email text string being parsed. 

    Why are you checking for 3 elements in the names array? 

    Given an email address such as Users_name@MyISP.com

    After splitting on the @ there will be two strings in names: 
    userName and domainName

    Try this:

    If names.Length = 2 Then
        userName = names(0)
        domainName = names(1)
    End If
    

    - Wayne

    Wednesday, November 7, 2018 9:00 PM