Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to create a program that will change each character in a string using vb

for example i have "I do"

I will be L, D will be G, O will be R and the output supposedly is

"L GR"

can you help me with this one, i search for codes but i found only one same character replaced.

i have tried using this pattern

Dim strMyText As String = "Visit Paris Today"
Dim strNewtext As String

strNewText = Replace ( strMyText, "Paris", "London")
Posted

I can't think of a way of doing it "all at once", but what you appear to be describing is a straight substitution of letters. You've mentioned VB6 as well as VB10 in your tags so I'm not sure what you're after. I've used VB.net but there are ways to convert this to VB6 if you really have to.

This is something I've cobbled together ... it works but there are better ways, this is just to get you going. Note, I haven't used Replace because you could end up replacing characters you've already substituted.

VB
'These arrays define the one-to-one mappings
Dim SourceChars As String() = New String() {" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
Dim ChangeChars As String() = New String() {" ", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C"}

'This is the test string
Dim test As String = "I do"

'This will be the output
Dim test2 As StringBuilder = New StringBuilder("")

'Step through the test string replacing each character
For i As Integer = 0 To test.Length - 1
    Dim testString As String = test.Substring(i, 1)
    Dim index As Integer = Array.FindIndex(SourceChars, Function(s) s.Contains(testString.ToUpper()))
    test2.Append(ChangeChars(index))
Next
Debug.Print(test2.ToString())
'Output is L GR

or using a Dictionary
VB
Dim test As String = "I do"
Dim kvp As Dictionary(Of String, String) = New Dictionary(Of String, String)
kvp.Add(" ", " ")
kvp.Add("A", "D")
'... etc
kvp.Add("I", "L")
kvp.Add("D", "G")
kvp.Add("O", "R")

Dim test2 As StringBuilder = New StringBuilder("")

For i As Integer = 0 To test.Length - 1
    test2.Append(kvp(test.Substring(i, 1).ToUpper()))
Next
Debug.Print(test2.ToString())
You ought to include some checks for characters not in the list and note that .ToUpper() on the lookup.
 
Share this answer
 
Comments
Maciej Los 22-Dec-14 12:35pm    
Very interesting trick, Caroline, +5!
It seems like you want to some encryption like Rot-N/Rot13

Take a look here http://www.vbforums.com/showthread.php?345183-Tutorial-Rot13-Rot-N-Encryption[^]

When using the example code you should replace "n=13" with "n=3"
 
Share this answer
 
Like this:
VB
Dim s As String = "Visit Paris Today"
        s = s.Replace("Paris", "London")
        Console.WriteLine(s)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900