COCO
COCO

Reputation: 158

Multiple string delimiters

I'm a total beginner in the VB language and I have a string in the format:

Smith, Alex ([email protected])

I'm attempting to isolate "asmith" from this string. I'm assuming that we would have to split at the "(" and then at the "@" and obtain the 1st index?

Any help is appreciated.

Upvotes: 1

Views: 159

Answers (3)

sujith karivelil
sujith karivelil

Reputation: 29016

Here is an option for you:

Dim input = "Smith, Alex ([email protected])"
Dim output= input.Split("("c, "@"c)(1)

Upvotes: 2

Heinzi
Heinzi

Reputation: 172230

I'm assuming that we would have to split at the "(" and then at the "@" and obtain the 1st index?

Your approach is correct. A literal translation of your algorithm to VB.NET would be:

Dim firstPartOfMailAddress = text.Split("("c)(1).Split("@"c)(0)

Explanation:

  • "("c is a character literal. Split expects a character, not a string (that would be "(").
  • Arrays are zero-based in VB.NET and indexed using the (...) operator. Thus, you take the second part (1) of the first split and the first part (0) of the second split.

Notes:

  • This code does not include any error handling and will fail horribly if the text does not contain the character (.
  • An alternative would be to extract the value using a regular expression, but if your are new to the language, splitting is a much easier option.

Upvotes: 4

use class :

Imports System.Globalization
Imports System.Text.RegularExpressions

Public Class RegexUtilities
    Dim invalid As Boolean = False

    Public Function IsValidEmail(strIn As String) As Boolean
        invalid = False
        If String.IsNullOrEmpty(strIn) Then Return False

        ' Use IdnMapping class to convert Unicode domain names.
        Try
            strIn = Regex.Replace(strIn, "(@)(.+)$", AddressOf Me.DomainMapper,
                                  RegexOptions.None, TimeSpan.FromMilliseconds(200))
        Catch e As RegexMatchTimeoutException
            Return False
        End Try

        If invalid Then Return False

        ' Return true if strIn is in valid e-mail format.
        Try
            Return Regex.IsMatch(strIn,
                   "^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                   "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
                   RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))
        Catch e As RegexMatchTimeoutException
            Return False
        End Try
    End Function

    Private Function DomainMapper(match As Match) As String
        ' IdnMapping class with default property values.
        Dim idn As New IdnMapping()

        Dim domainName As String = match.Groups(2).Value
        Try
            domainName = idn.GetAscii(domainName)
        Catch e As ArgumentException
            invalid = True
        End Try
        Return match.Groups(1).Value + domainName
    End Function
End Class

and use code in form and customzition :

Dim util As New RegexUtilities()
        Dim emailAddresses() As String = {"[email protected]", "[email protected]",
                                           "[email protected]", "[email protected]",
                                           "[email protected]", "js#[email protected]",
                                           "j_9@[129.126.118.1]", "[email protected]",
                                           "js*@proseware.com", "[email protected]",
                                           "[email protected]", "[email protected]",
                                           """j\""s\""""@proseware.com", "js@contoso.中国"}

        For Each emailAddress As String In emailAddresses
            If util.IsValidEmail(emailAddress) Then
                Console.WriteLine("Valid: {0}", emailAddress)
            Else
                Console.WriteLine("Invalid: {0}", emailAddress)
            End If
        Next

Upvotes: 1

Related Questions