Results 1 to 7 of 7

Thread: Replace Alphabet

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Posts
    346

    Replace Alphabet

    How could I replace certain letters of a string with diferent ones without useing a lot of if statments. So just replace each letter or number with a diferent character. Any suggestions would be nice.

  2. #2
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Replace Alphabet

    Code:
    String.Replace("a", "!")
    String.Replace ("3","f")
    String.Replace ("S","g")
    ....
    However, I can't see the solution being as simple as that. If I misunderstood, please can you explain in a bit more detail, perhaps with an example of a "before" and "after" string?

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Replace Alphabet

    That is a little bit vague. Is it intended to be a code of sorts (eg. ROT-13) or should the replacements be random? Do you want to stick with alphanumerics or will any character do?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Posts
    346

    Re: Replace Alphabet

    What Espanolita said would work, and I could do that. But is there a way to make that easier? without haveing to do every character you wish to replace
    ?

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Replace Alphabet

    Well that's why I asked if random replacement was acceptable. If you want a specific replacement for each character then at some point you're going to need to define each one unless there is some kind of 'formula' (such as ROT-13) that can be applied. It really would help to have an idea of what this procedure is meant to acheive.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Replace Alphabet

    Just use String.Replace()


    Example:
    Code:
    Dim str As String = "Here is your sentence."
    
    Return str.Replace("e", "3") ' will output "H3r3 is your s3nt3nc3."
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  7. #7
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Replace Alphabet

    Or you could try something like:
    vb.net Code:
    1. 'Will replace any occurence of a character in replace_chars with index i that is present in source
    2.     'with the corresponding character in replace_with at index i.
    3.     'Thus "Abe Lincoln", "AaEeLl", "443311" will result in "4b3 1inco1n"
    4.     Private Function replace(source As String, replace_chars As String, replace_with As String) As String
    5.         If replace_chars.Length <> replace_with.Length Then Return String.Empty 'ERROR
    6.  
    7.         Dim d As New SortedList(Of Char, Char)
    8.  
    9.         For i As Integer = 0 To replace_chars.Length - 1
    10.             d.Add(replace_chars(i), replace_with(i))
    11.         Next
    12.  
    13.         Return String.Concat(source.Select(Function(c) If(d.ContainsKey(c), d(c), c)))
    14.     End Function
    if you have to replace a lot of characters in a long string (will only scan the source-string once, but has the overhead of creating a sorted list).

    Usage:
    Code:
    MessageBox.Show(replace("Abe Lincoln", "AaEeLl", "443311"))
    Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

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