Results 1 to 6 of 6

Thread: Question Using String Replace

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    295

    Question Question Using String Replace

    Hello all,

    I have to convert an Email address before I search a database. I have to Change the (.) Period to an (_) Underscore before I search and the string replace changes ALL the periods to underscore.

    I need to replace the period in the Name and not in the .COM


    This ([email protected]) needs to be ([email protected])

    Using the replace method it turns into this

    some_one@here_com

    Is there a way to change the first occurrence only?

    -NJ

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Question Using String Replace

    Is it just the 1st occurrence of the period? What if the email address is [email protected]?

    This extension method will replace all periods with underscores before the @.

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.  
    5.         Dim email = "[email protected]"
    6.         email.ReplacePeriodEmailUser()
    7.  
    8.     End Sub
    9. End Class
    10.  
    11. Public Module StringExtensions
    12.  
    13.     <System.Runtime.CompilerServices.Extension()>
    14.     Public Sub ReplacePeriodEmailUser(ByRef text As String)
    15.         Dim split = text.Split("@"c)
    16.         text = String.Join("@"c, split(0).Replace("."c, "_"c), split(1))
    17.     End Sub
    18.  
    19. End Module
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,375

    Re: Question Using String Replace

    Well regex is certainly one way, here is another:
    Code:
            'Some email
            Dim email_string As String = "[email protected]"
    
            'Get first instance of @
            Dim at_sign As Integer = email_string.IndexOf("@")
    
            'Loop from the beginning to the @ sign
            For i As Integer = 0 To at_sign
                'If that certain letter is a . then...
                If email_string.Substring(i, 1) = "." Then
                    'Replace it with an _
                    email_string = email_string.Substring(0, i) & "_" & email_string.Substring(i + 1)
                End If
            Next
    Edit - MattP beat me!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    295

    Re: Question Using String Replace

    That is awesome!!!

    I never knew of the text.split option.

    Thanks,

    -NJ

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Question Using String Replace

    Quote Originally Posted by NJDevils28 View Post
    I never knew of the text.split option.
    Here's the documentation: String.Split Method
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Question Using String Replace

    You can also use string builder.
    Code:
            Dim s As String = "[email protected]"
            Dim cpy As New System.Text.StringBuilder(s)
    
            For x As Integer = 0 To cpy.Length - 1
                If cpy(x) = "."c Then
                    cpy(x) = "_"c
                    Exit For
                End If
            Next
    
            s = cpy.ToString
    This only works if there is a "." before the "@".
    Last edited by dbasnett; May 23rd, 2013 at 09:09 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width