Andrew Kilburn
Andrew Kilburn

Reputation: 41

How can I convert a string into different characters?

I have looked on the web and I cannot find anything that helps me, all I can find is changing the characters into ASCII or Hexadecimal. However I would like to do it a different way. For example, say the string that got passed in was abcdef, I would like to have a key which changes these characters into another string such as qwpolz. Is there an easier way than declaring each character in the alphabet to be another character like:

Dim sText As String = "Hello"
Dim sEncode As String = ""
Dim iLength As Integer
Dim i As Integer
iLength = Len(sText)

For i = 1 To iLength
    sEncode = sEncode ????
Next
    Return sEncode

And then have a very lengthy loop which checks for these loops? There must be a much simpler way. Can anybody help by pointing me in the right direction?

Edit: Why downvote? Seriously, it's a legitimate question. Instead of downvoting for no reason, just move onto another question.

Upvotes: 0

Views: 104

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460068

Sounds as if you want to modify a string by replacing each character with a different character according to a mapping table. An efficient approach is to use a Dictionary(Of Char, Char). But easier to write and maintain is something like this:

Shared ReadOnly replaceChars As String = "abcdef"
Shared ReadOnly withChars As String = "qwpolz"

Public Shared Function ReplaceAll(input As String) As String
    Dim newChars = From c In input
                   Let index = replaceChars.IndexOf(c)
                   Select If(index >= 0, withChars(index), c)
    Return String.Concat(newChars)
End Function

So the first string contains the chars that you want to replace and the second the replacement characters. Both strings must have the same length.

If you want to support case insensitivity:

Public Shared Function ReplaceAll(input As String, comparison As StringComparison) As String
    Dim newChars = From c In input
                   Let index = replaceChars.IndexOf(c.ToString(), comparison)
                   Select If(index >= 0, withChars(index), c)
    Return String.Concat(newChars)
End Function

Note that this is also a loop. There is no way to avoid some kind of loops if you want to replace multiple characters or strings.

Upvotes: 1

Margus
Margus

Reputation: 20038

Well actually, this sounds like a Caesar sipher

enter image description here

Private Overloads Shared Function Encrypt(ByVal ch As Char, ByVal code As Integer) As Char
    If Not Char.IsLetter(ch) Then
        Return ch
    End If
    Dim offset As Char = IIf(Char.IsUpper(ch), "A", "a")
    Return CType((((ch + (code - offset)) Mod 26) + offset),Char)
End Function

Private Overloads Shared Function Encrypt(ByVal input As String, ByVal code As Integer) As String
    Return New String(input.ToCharArray.Select(() => {  }, Encrypt(ch, code)).ToArray)
End Function

Private Shared Function Decrypt(ByVal input As String, ByVal code As Integer) As String
    Return Encrypt(input, (26 - code))
End Function

Note that this assumes, that you use English alphabet. In general case where for example you have 'ä', 'ö', 'š', 'ž', 'ß', 'õ', 'ü' etc. this would not work. In that case it is simpler to just create a list/dictionary of your ordered alphabet and use it.

Example use:

encrypted = Encrypt(sText, 5)
decypted = Decrypt(encrypted, 5)

Upvotes: 1

Related Questions